Skip to content

Commit 487e156

Browse files
Terraform Team Automationsagarp337
authored andcommitted
Added - Support for unified agent config CRI parser
1 parent f029f7c commit 487e156

13 files changed

+444
-100
lines changed

examples/logging/log_agent_configuration/log_agent_configuration.tf

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ variable "unified_agent_configuration_description" {
1212
}
1313

1414
variable "unified_agent_configuration_display_name" {
15-
default = "tf-agentConfigName"
15+
default = "tf-agentConfigName1"
1616
}
1717

1818
variable "unified_agent_configuration_freeform_tags" {
@@ -196,6 +196,25 @@ resource "oci_logging_unified_agent_configuration" "test_unified_agent_configura
196196
#Required
197197
source_type = var.unified_agent_configuration_service_configuration_sources_source_type
198198

199+
#Optional
200+
# channels for windows only
201+
# channels = var.unified_agent_configuration_service_configuration_sources_channels
202+
name = var.unified_agent_configuration_service_configuration_sources_name
203+
parser {
204+
parser_type = "CRI"
205+
is_merge_cri_fields = false
206+
nested_parser {
207+
time_format = "%Y-%m-%dT%H:%M:%S.%L%z"
208+
field_time_key = "time"
209+
is_keep_time_key = true
210+
}
211+
}
212+
paths = ["/var/log/*"]
213+
}
214+
sources {
215+
#Required
216+
source_type = var.unified_agent_configuration_service_configuration_sources_source_type
217+
199218
#Optional
200219
# channels for windows only
201220
# channels = var.unified_agent_configuration_service_configuration_sources_channels

examples/logging/provider.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ provider "oci" {
3131
user_ocid = var.user_ocid
3232
fingerprint = var.fingerprint
3333
private_key_path = var.private_key_path
34-
# version = "4.100.0"
34+
# version = "4.119.0"
3535
}
3636

internal/integrationtest/logging_unified_agent_configuration_test.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,25 @@ var (
104104
"parser": acctest.RepresentationGroup{RepType: acctest.Required, Group: LoggingUnifiedAgentConfigurationServiceConfigurationSourcesParserRepresentation},
105105
//"channels": acctest.Representation{RepType: acctest.Required, Create: []string{`Security`}, Update: []string{`Security`, `Application`}},
106106
}
107+
108+
// Add new test configs here
109+
// CRI parser configs
110+
LoggingUnifiedAgentConfigurationServiceConfigurationSourcesCriParserNestedParserRepresentation = map[string]interface{}{
111+
"time_format": acctest.Representation{RepType: acctest.Optional, Create: `%Y-%m-%dT%H:%M:%S.%L%z`, Update: `%Y-%m-%d %H:%M:%S.%L%z`},
112+
"field_time_key": acctest.Representation{RepType: acctest.Optional, Create: `time`, Update: `time1`},
113+
"is_keep_time_key": acctest.Representation{RepType: acctest.Optional, Create: `true`, Update: `false`},
114+
}
115+
116+
LoggingUnifiedAgentConfigurationServiceConfigurationSourcesCriParserRepresentation = map[string]interface{}{
117+
"parser_type": acctest.Representation{RepType: acctest.Required, Create: `CRI`},
118+
"is_merge_cri_fields": acctest.Representation{RepType: acctest.Optional, Create: `true`, Update: `false`},
119+
"nested_parser": acctest.RepresentationGroup{RepType: acctest.Optional, Group: LoggingUnifiedAgentConfigurationServiceConfigurationSourcesCriParserNestedParserRepresentation},
120+
}
121+
122+
LoggingUnifiedAgentConfigurationCriRepresentation = acctest.GetUpdatedRepresentationCopy(
123+
"service_configuration.sources.parser",
124+
acctest.RepresentationGroup{RepType: acctest.Required, Group: LoggingUnifiedAgentConfigurationServiceConfigurationSourcesCriParserRepresentation},
125+
LoggingUnifiedAgentConfigurationRepresentation)
107126
)
108127

109128
// issue-routing-tag: logging/default
@@ -129,6 +148,90 @@ func TestLoggingUnifiedAgentConfigurationResource_basic(t *testing.T) {
129148
acctest.GenerateResourceFromRepresentationMap("oci_logging_unified_agent_configuration", "test_unified_agent_configuration", acctest.Optional, acctest.Create, LoggingUnifiedAgentConfigurationRepresentation), "logging", "unifiedAgentConfiguration", t)
130149

131150
acctest.ResourceTest(t, testAccCheckLoggingUnifiedAgentConfigurationDestroy, []resource.TestStep{
151+
152+
// Add new tests here
153+
// CRI parser test required
154+
{
155+
Config: config + compartmentIdVariableStr + LoggingUnifiedAgentConfigurationResourceDependencies +
156+
acctest.GenerateResourceFromRepresentationMap("oci_logging_unified_agent_configuration", "test_unified_agent_configuration", acctest.Required, acctest.Create, LoggingUnifiedAgentConfigurationCriRepresentation),
157+
Check: acctest.ComposeAggregateTestCheckFuncWrapper(
158+
resource.TestCheckResourceAttr(resourceName, "compartment_id", compartmentId),
159+
resource.TestCheckResourceAttr(resourceName, "is_enabled", "true"),
160+
resource.TestCheckResourceAttr(resourceName, "service_configuration.#", "1"),
161+
resource.TestCheckResourceAttr(resourceName, "service_configuration.0.configuration_type", "LOGGING"),
162+
resource.TestCheckResourceAttr(resourceName, "service_configuration.0.destination.#", "1"),
163+
resource.TestCheckResourceAttrSet(resourceName, "service_configuration.0.destination.0.log_object_id"),
164+
resource.TestCheckResourceAttr(resourceName, "service_configuration.0.sources.#", "1"),
165+
//resource.TestCheckResourceAttr(resourceName, "service_configuration.0.sources.0.channels.#", "1"),
166+
resource.TestCheckResourceAttr(resourceName, "service_configuration.0.sources.0.name", "name"),
167+
resource.TestCheckResourceAttr(resourceName, "service_configuration.0.sources.0.paths.#", "1"),
168+
resource.TestCheckResourceAttr(resourceName, "service_configuration.0.sources.0.source_type", "LOG_TAIL"),
169+
resource.TestCheckResourceAttr(resourceName, "service_configuration.0.sources.0.parser.0.parser_type", "CRI"),
170+
171+
func(s *terraform.State) (err error) {
172+
_, err = acctest.FromInstanceState(s, resourceName, "id")
173+
return err
174+
},
175+
),
176+
},
177+
// CRI parser test optional
178+
{
179+
Config: config + compartmentIdVariableStr + LoggingUnifiedAgentConfigurationResourceDependencies +
180+
acctest.GenerateResourceFromRepresentationMap("oci_logging_unified_agent_configuration", "test_unified_agent_configuration", acctest.Optional, acctest.Create, LoggingUnifiedAgentConfigurationCriRepresentation),
181+
Check: acctest.ComposeAggregateTestCheckFuncWrapper(
182+
resource.TestCheckResourceAttr(resourceName, "compartment_id", compartmentId),
183+
resource.TestCheckResourceAttr(resourceName, "is_enabled", "true"),
184+
resource.TestCheckResourceAttr(resourceName, "service_configuration.#", "1"),
185+
resource.TestCheckResourceAttr(resourceName, "service_configuration.0.configuration_type", "LOGGING"),
186+
resource.TestCheckResourceAttr(resourceName, "service_configuration.0.destination.#", "1"),
187+
resource.TestCheckResourceAttrSet(resourceName, "service_configuration.0.destination.0.log_object_id"),
188+
resource.TestCheckResourceAttr(resourceName, "service_configuration.0.sources.#", "1"),
189+
//resource.TestCheckResourceAttr(resourceName, "service_configuration.0.sources.0.channels.#", "1"),
190+
resource.TestCheckResourceAttr(resourceName, "service_configuration.0.sources.0.name", "name"),
191+
resource.TestCheckResourceAttr(resourceName, "service_configuration.0.sources.0.paths.#", "1"),
192+
resource.TestCheckResourceAttr(resourceName, "service_configuration.0.sources.0.source_type", "LOG_TAIL"),
193+
resource.TestCheckResourceAttr(resourceName, "service_configuration.0.sources.0.parser.0.parser_type", "CRI"),
194+
resource.TestCheckResourceAttr(resourceName, "service_configuration.0.sources.0.parser.0.is_merge_cri_fields", "true"),
195+
resource.TestCheckResourceAttr(resourceName, "service_configuration.0.sources.0.parser.0.nested_parser.0.time_format", "%Y-%m-%dT%H:%M:%S.%L%z"),
196+
resource.TestCheckResourceAttr(resourceName, "service_configuration.0.sources.0.parser.0.nested_parser.0.field_time_key", "time"),
197+
resource.TestCheckResourceAttr(resourceName, "service_configuration.0.sources.0.parser.0.nested_parser.0.is_keep_time_key", "true"),
198+
199+
func(s *terraform.State) (err error) {
200+
_, err = acctest.FromInstanceState(s, resourceName, "id")
201+
return err
202+
},
203+
),
204+
},
205+
// CRI parser test optional update
206+
{
207+
Config: config + compartmentIdVariableStr + LoggingUnifiedAgentConfigurationResourceDependencies +
208+
acctest.GenerateResourceFromRepresentationMap("oci_logging_unified_agent_configuration", "test_unified_agent_configuration", acctest.Optional, acctest.Update, LoggingUnifiedAgentConfigurationCriRepresentation),
209+
Check: acctest.ComposeAggregateTestCheckFuncWrapper(
210+
resource.TestCheckResourceAttr(resourceName, "compartment_id", compartmentId),
211+
resource.TestCheckResourceAttr(resourceName, "is_enabled", "false"),
212+
resource.TestCheckResourceAttr(resourceName, "service_configuration.#", "1"),
213+
resource.TestCheckResourceAttr(resourceName, "service_configuration.0.configuration_type", "LOGGING"),
214+
resource.TestCheckResourceAttr(resourceName, "service_configuration.0.destination.#", "1"),
215+
resource.TestCheckResourceAttrSet(resourceName, "service_configuration.0.destination.0.log_object_id"),
216+
resource.TestCheckResourceAttr(resourceName, "service_configuration.0.sources.#", "1"),
217+
//resource.TestCheckResourceAttr(resourceName, "service_configuration.0.sources.0.channels.#", "1"),
218+
resource.TestCheckResourceAttr(resourceName, "service_configuration.0.sources.0.name", "name"),
219+
resource.TestCheckResourceAttr(resourceName, "service_configuration.0.sources.0.paths.#", "1"),
220+
resource.TestCheckResourceAttr(resourceName, "service_configuration.0.sources.0.source_type", "LOG_TAIL"),
221+
resource.TestCheckResourceAttr(resourceName, "service_configuration.0.sources.0.parser.0.parser_type", "CRI"),
222+
resource.TestCheckResourceAttr(resourceName, "service_configuration.0.sources.0.parser.0.is_merge_cri_fields", "false"),
223+
resource.TestCheckResourceAttr(resourceName, "service_configuration.0.sources.0.parser.0.nested_parser.0.time_format", "%Y-%m-%d %H:%M:%S.%L%z"),
224+
resource.TestCheckResourceAttr(resourceName, "service_configuration.0.sources.0.parser.0.nested_parser.0.field_time_key", "time1"),
225+
resource.TestCheckResourceAttr(resourceName, "service_configuration.0.sources.0.parser.0.nested_parser.0.is_keep_time_key", "false"),
226+
227+
func(s *terraform.State) (err error) {
228+
_, err = acctest.FromInstanceState(s, resourceName, "id")
229+
return err
230+
},
231+
),
232+
},
233+
234+
// Don't change below tests
132235
// 0. verify Create
133236
{
134237
Config: config + compartmentIdVariableStr + LoggingUnifiedAgentConfigurationResourceDependencies +

internal/service/logging/logging_unified_agent_configuration_resource.go

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ func LoggingUnifiedAgentConfigurationResource() *schema.Resource {
122122
"APACHE2",
123123
"APACHE_ERROR",
124124
"AUDITD",
125+
"CRI",
125126
"CSV",
126127
"GROK",
127128
"JSON",
@@ -184,6 +185,12 @@ func LoggingUnifiedAgentConfigurationResource() *schema.Resource {
184185
Optional: true,
185186
Computed: true,
186187
},
188+
"is_merge_cri_fields": {
189+
Type: schema.TypeBool,
190+
Optional: true,
191+
Computed: true,
192+
DiffSuppressFunc: criDiffSuppressfunc,
193+
},
187194
"is_null_empty_string": {
188195
Type: schema.TypeBool,
189196
Optional: true,
@@ -222,6 +229,40 @@ func LoggingUnifiedAgentConfigurationResource() *schema.Resource {
222229
Optional: true,
223230
Computed: true,
224231
},
232+
"nested_parser": {
233+
Type: schema.TypeList,
234+
Optional: true,
235+
Computed: true,
236+
MaxItems: 1,
237+
MinItems: 1,
238+
Elem: &schema.Resource{
239+
Schema: map[string]*schema.Schema{
240+
241+
// Optional
242+
"field_time_key": {
243+
Type: schema.TypeString,
244+
Optional: true,
245+
Computed: true,
246+
},
247+
"time_format": {
248+
Type: schema.TypeString,
249+
Optional: true,
250+
Computed: true,
251+
},
252+
"time_type": {
253+
Type: schema.TypeString,
254+
Optional: true,
255+
Computed: true,
256+
},
257+
"is_keep_time_key": {
258+
Type: schema.TypeBool,
259+
Optional: true,
260+
Computed: true,
261+
},
262+
},
263+
},
264+
DiffSuppressFunc: criDiffSuppressfunc,
265+
},
225266
"null_value_pattern": {
226267
Type: schema.TypeString,
227268
Optional: true,
@@ -316,6 +357,8 @@ func LoggingUnifiedAgentConfigurationResource() *schema.Resource {
316357
},
317358
},
318359

360+
// Optional
361+
319362
// Computed
320363
},
321364
},
@@ -1077,6 +1120,7 @@ func (s *LoggingUnifiedAgentConfigurationResourceCrud) mapToUnifiedAgentParser(f
10771120
} else {
10781121
parserType = "" // default value
10791122
}
1123+
10801124
switch strings.ToLower(parserType) {
10811125
case strings.ToLower("APACHE2"):
10821126
details := oci_logging.UnifiedAgentApache2Parser{}
@@ -1168,6 +1212,50 @@ func (s *LoggingUnifiedAgentConfigurationResourceCrud) mapToUnifiedAgentParser(f
11681212
details.Types = tfresource.ObjectMapToStringMap(types.(map[string]interface{}))
11691213
}
11701214
baseObject = details
1215+
case strings.ToLower("CRI"):
1216+
details := oci_logging.UnifiedAgentCriParser{}
1217+
if isMergeCriFields, ok := s.D.GetOkExists(fmt.Sprintf(fieldKeyFormat, "is_merge_cri_fields")); ok {
1218+
tmp := isMergeCriFields.(bool)
1219+
details.IsMergeCriFields = &tmp
1220+
}
1221+
if nestedParser, ok := s.D.GetOkExists(fmt.Sprintf(fieldKeyFormat, "nested_parser")); ok {
1222+
if tmpList := nestedParser.([]interface{}); len(tmpList) > 0 {
1223+
fieldKeyFormatNextLevel := fmt.Sprintf("%s.%d.%%s", fmt.Sprintf(fieldKeyFormat, "nested_parser"), 0)
1224+
tmp, err := s.mapToUnifiedJsonParser(fieldKeyFormatNextLevel)
1225+
if err != nil {
1226+
return details, fmt.Errorf("unable to convert nested_parser, encountered error: %v", err)
1227+
}
1228+
details.NestedParser = &tmp
1229+
}
1230+
}
1231+
if fieldTimeKey, ok := s.D.GetOkExists(fmt.Sprintf(fieldKeyFormat, "field_time_key")); ok {
1232+
tmp := fieldTimeKey.(string)
1233+
details.FieldTimeKey = &tmp
1234+
}
1235+
if isEstimateCurrentEvent, ok := s.D.GetOkExists(fmt.Sprintf(fieldKeyFormat, "is_estimate_current_event")); ok {
1236+
tmp := isEstimateCurrentEvent.(bool)
1237+
details.IsEstimateCurrentEvent = &tmp
1238+
}
1239+
if isKeepTimeKey, ok := s.D.GetOkExists(fmt.Sprintf(fieldKeyFormat, "is_keep_time_key")); ok {
1240+
tmp := isKeepTimeKey.(bool)
1241+
details.IsKeepTimeKey = &tmp
1242+
}
1243+
if isNullEmptyString, ok := s.D.GetOkExists(fmt.Sprintf(fieldKeyFormat, "is_null_empty_string")); ok {
1244+
tmp := isNullEmptyString.(bool)
1245+
details.IsNullEmptyString = &tmp
1246+
}
1247+
if nullValuePattern, ok := s.D.GetOkExists(fmt.Sprintf(fieldKeyFormat, "null_value_pattern")); ok {
1248+
tmp := nullValuePattern.(string)
1249+
details.NullValuePattern = &tmp
1250+
}
1251+
if timeoutInMilliseconds, ok := s.D.GetOkExists(fmt.Sprintf(fieldKeyFormat, "timeout_in_milliseconds")); ok {
1252+
tmp := timeoutInMilliseconds.(int)
1253+
details.TimeoutInMilliseconds = &tmp
1254+
}
1255+
if types, ok := s.D.GetOkExists(fmt.Sprintf(fieldKeyFormat, "types")); ok {
1256+
details.Types = tfresource.ObjectMapToStringMap(types.(map[string]interface{}))
1257+
}
1258+
baseObject = details
11711259
case strings.ToLower("CSV"):
11721260
details := oci_logging.UnifiedAgentCsvParser{}
11731261
if delimiter, ok := s.D.GetOkExists(fmt.Sprintf(fieldKeyFormat, "delimiter")); ok {
@@ -1693,6 +1781,17 @@ func UnifiedAgentParserToMap(obj *oci_logging.UnifiedAgentParser) map[string]int
16931781
if v.Types != nil {
16941782
result["types"] = tfresource.StringMapToObjectMap(v.Types)
16951783
}
1784+
case oci_logging.UnifiedAgentCriParser:
1785+
result["parser_type"] = "CRI"
1786+
1787+
if v.IsMergeCriFields != nil {
1788+
result["is_merge_cri_fields"] = bool(*v.IsMergeCriFields)
1789+
}
1790+
1791+
if v.NestedParser != nil {
1792+
result["nested_parser"] = []interface{}{UnifiedJsonParserToMap(v.NestedParser)}
1793+
}
1794+
16961795
case oci_logging.UnifiedAgentCsvParser:
16971796
result["parser_type"] = "CSV"
16981797
if v.FieldTimeKey != nil {
@@ -2098,6 +2197,51 @@ func UnifiedAgentServiceConfigurationDetailsToMap(obj *oci_logging.UnifiedAgentS
20982197
return result
20992198
}
21002199

2200+
func (s *LoggingUnifiedAgentConfigurationResourceCrud) mapToUnifiedJsonParser(fieldKeyFormat string) (oci_logging.UnifiedJsonParser, error) {
2201+
result := oci_logging.UnifiedJsonParser{}
2202+
2203+
if timeFormat, ok := s.D.GetOkExists(fmt.Sprintf(fieldKeyFormat, "time_format")); ok {
2204+
tmp := timeFormat.(string)
2205+
result.TimeFormat = &tmp
2206+
}
2207+
2208+
if timeType, ok := s.D.GetOkExists(fmt.Sprintf(fieldKeyFormat, "time_type")); ok {
2209+
result.TimeType = oci_logging.UnifiedJsonParserTimeTypeEnum(timeType.(string))
2210+
}
2211+
2212+
if fieldTimeKey, ok := s.D.GetOkExists(fmt.Sprintf(fieldKeyFormat, "field_time_key")); ok {
2213+
tmp := fieldTimeKey.(string)
2214+
result.FieldTimeKey = &tmp
2215+
}
2216+
2217+
if isKeepTimeKey, ok := s.D.GetOkExists(fmt.Sprintf(fieldKeyFormat, "is_keep_time_key")); ok {
2218+
tmp := isKeepTimeKey.(bool)
2219+
result.IsKeepTimeKey = &tmp
2220+
}
2221+
2222+
return result, nil
2223+
}
2224+
2225+
func UnifiedJsonParserToMap(obj *oci_logging.UnifiedJsonParser) map[string]interface{} {
2226+
result := map[string]interface{}{}
2227+
2228+
if obj.TimeFormat != nil {
2229+
result["time_format"] = string(*obj.TimeFormat)
2230+
}
2231+
2232+
result["time_type"] = string(obj.TimeType)
2233+
2234+
if obj.FieldTimeKey != nil {
2235+
result["field_time_key"] = *obj.FieldTimeKey
2236+
}
2237+
2238+
if obj.IsKeepTimeKey != nil {
2239+
result["is_keep_time_key"] = *obj.IsKeepTimeKey
2240+
}
2241+
2242+
return result
2243+
}
2244+
21012245
func (s *LoggingUnifiedAgentConfigurationResourceCrud) updateCompartment(compartment interface{}) error {
21022246
changeCompartmentRequest := oci_logging.ChangeUnifiedAgentConfigurationCompartmentRequest{}
21032247

@@ -2117,3 +2261,33 @@ func (s *LoggingUnifiedAgentConfigurationResourceCrud) updateCompartment(compart
21172261
workId := response.OpcWorkRequestId
21182262
return s.getUnifiedAgentConfigurationFromWorkRequest(workId, tfresource.GetRetryPolicy(s.DisableNotFoundRetries, "logging"), oci_logging.ActionTypesRelated, s.D.Timeout(schema.TimeoutUpdate))
21192263
}
2264+
2265+
func criDiffSuppressfunc(k string, old string, new string, d *schema.ResourceData) bool {
2266+
// k = "service_configuration.0.sources.0.parser.0.xxx"
2267+
var parserTypeStr string
2268+
dotIndex := findNthDotIndex(k, 6)
2269+
if dotIndex != -1 {
2270+
parserTypePath := k[:dotIndex+1] + "parser_type"
2271+
if parserType, ok := d.GetOkExists(parserTypePath); ok {
2272+
parserTypeStr = parserType.(string)
2273+
if strings.ToLower(parserTypeStr) == "cri" {
2274+
return false
2275+
}
2276+
}
2277+
}
2278+
log.Printf("Diff suppress for parser_type: %v. k: %v, old: %v, new: %v", parserTypeStr, k, old, new)
2279+
return true
2280+
}
2281+
2282+
func findNthDotIndex(s string, n int) int {
2283+
count := 0
2284+
for i, char := range s {
2285+
if char == '.' {
2286+
count++
2287+
if count == n {
2288+
return i
2289+
}
2290+
}
2291+
}
2292+
return -1 // Return -1 if the ith dot is not found
2293+
}

0 commit comments

Comments
 (0)