Skip to content

Commit

Permalink
Merge pull request #16 from eskabar/add-aws-log-source-filters
Browse files Browse the repository at this point in the history
Add Support for Processing Rules
  • Loading branch information
brandonstevens authored Aug 20, 2019
2 parents bea892c + 3d52ac4 commit 7a2dac8
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 21 deletions.
22 changes: 22 additions & 0 deletions sumologic/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package sumologic

import (
"github.com/nextgenhealthcare/sumologic-sdk-go"
)

func appendFilters(sumologicFilters []interface{}) []sumologic.Filter {
filters := make([]sumologic.Filter, 0)
for _, v := range sumologicFilters {
v := v.(map[string]interface{})

f := sumologic.Filter{
FilterType: v["filter_type"].(string),
Name: v["name"].(string),
Regexp: v["regexp"].(string),
}

filters = append(filters, f)
}

return filters
}
54 changes: 51 additions & 3 deletions sumologic/resource_sumologic_aws_log_source.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package sumologic

import (
"fmt"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/nextgenhealthcare/sumologic-sdk-go"
"strconv"
"strings"
"time"
)

Expand Down Expand Up @@ -54,7 +56,7 @@ func resourceAWSLogSource() *schema.Resource {
"paused": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Computed: true,
Default: false,
},
"cutoff_relative_time": &schema.Schema{
Type: schema.TypeString,
Expand All @@ -64,12 +66,12 @@ func resourceAWSLogSource() *schema.Resource {
"multiline_processing_enabled": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Computed: true,
Default: false,
},
"use_autoline_matching": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Computed: true,
Default: false,
},
"manual_prefix_regexp": &schema.Schema{
Type: schema.TypeString,
Expand All @@ -81,6 +83,27 @@ func resourceAWSLogSource() *schema.Resource {
Optional: true,
Computed: true,
},
"filter": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"filter_type": &schema.Schema{
Type: schema.TypeString,
Required: true,
ValidateFunc: filterTypeValidation,
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"regexp": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
},
},
"third_party_ref": {
Type: schema.TypeList,
MaxItems: 1,
Expand Down Expand Up @@ -165,7 +188,9 @@ func resourceAWSLogSourceCreate(d *schema.ResourceData, m interface{}) error {
ThirdPartyRef: sumologic.AWSBucketThirdPartyRef{
Resources: make([]sumologic.AWSBucketResource, 0),
},
Filters: make([]sumologic.Filter, 0),
}

a := sumologic.AWSBucketResource{
ServiceType: d.Get("third_party_ref.0.resources.0.service_type").(string),
Path: sumologic.AWSBucketPath{
Expand All @@ -178,8 +203,13 @@ func resourceAWSLogSourceCreate(d *schema.ResourceData, m interface{}) error {
RoleARN: d.Get("third_party_ref.0.resources.0.authentication.0.role_arn").(string),
},
}

s.ThirdPartyRef.Resources = append(s.ThirdPartyRef.Resources, a)

sumologicFilters := d.Get("filter").(*schema.Set).List()

s.Filters = appendFilters(sumologicFilters)

// Retry due to IAM eventual consistency
var out *sumologic.AWSLogSource
err := resource.Retry(2*time.Minute, func() *resource.RetryError {
Expand Down Expand Up @@ -233,6 +263,7 @@ func resourceAWSLogSourceRead(d *schema.ResourceData, m interface{}) error {
d.Set("third_party_ref.0.resources.0.path.0.path_expression", source.ThirdPartyRef.Resources[0].Path.PathExpression)
d.Set("third_party_ref.0.resources.0.authentication.0.type", source.ThirdPartyRef.Resources[0].Authentication.Type)
d.Set("third_party_ref.0.resources.0.authentication.0.role_arn", source.ThirdPartyRef.Resources[0].Authentication.RoleARN)
d.Set("filter", source.Filters)

return nil
}
Expand All @@ -259,6 +290,7 @@ func resourceAWSLogSourceUpdate(d *schema.ResourceData, m interface{}) error {
ThirdPartyRef: sumologic.AWSBucketThirdPartyRef{
Resources: make([]sumologic.AWSBucketResource, 0),
},
Filters: make([]sumologic.Filter, 0),
}
a := sumologic.AWSBucketResource{
ServiceType: d.Get("third_party_ref.0.resources.0.service_type").(string),
Expand All @@ -274,6 +306,10 @@ func resourceAWSLogSourceUpdate(d *schema.ResourceData, m interface{}) error {
}
source.ThirdPartyRef.Resources = append(source.ThirdPartyRef.Resources, a)

sumologicFilters := d.Get("filter").(*schema.Set).List()

source.Filters = appendFilters(sumologicFilters)

_, etag, _ := client.GetAWSLogSource(d.Get("collector_id").(int), id)

// Retry due to IAM eventual consistency
Expand Down Expand Up @@ -311,3 +347,15 @@ func resourceAWSLogSourceDelete(d *schema.ResourceData, m interface{}) error {

return nil
}

func filterTypeValidation(val interface{}, key string) (warns []string, errs []error) {
v := strings.ToLower(val.(string))
valid_types := [5]string{"exclude", "include", "mask", "hash", "forward"}
for _, t := range valid_types {
if t == v {
return
}
}
errs = append(errs, fmt.Errorf("filter_type must be one of %v. Found %v", valid_types, v))
return
}
11 changes: 11 additions & 0 deletions sumologic/resource_sumologic_aws_log_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,17 @@ resource "sumologic_aws_log_source" "source" {
role_arn = "%[4]s"
}
}
}
filter {
filter_type = "Exclude"
name = "No INFO"
regexp = "(?s).*\\[INFO\\].*(?s)"
}
filter {
filter_type = "Exclude"
name = "No DEBUG"
regexp = "(?s).*\\[DEBUG\\].*(?s)"
}
}
`, r, sumoLogicLogContentType, awsS3Bucket, awsSumoLogicIAMRoleArn)
Expand Down
43 changes: 39 additions & 4 deletions sumologic/resource_sumologic_http_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ func resourceHTTPSource() *schema.Resource {
"multiline_processing_enabled": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Computed: true,
Default: false,
},
"use_autoline_matching": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Computed: true,
Default: false,
},
"manual_prefix_regexp": &schema.Schema{
Type: schema.TypeString,
Expand All @@ -65,14 +65,35 @@ func resourceHTTPSource() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"filter": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"filter_type": &schema.Schema{
Type: schema.TypeString,
Required: true,
ValidateFunc: filterTypeValidation,
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"regexp": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
},
},
},
}
}

func resourceHTTPSourceCreate(d *schema.ResourceData, m interface{}) error {
client := m.(*sumologic.Client)

source, err := client.CreateHTTPSource(d.Get("collector_id").(int), sumologic.HTTPSource{
s := sumologic.HTTPSource{
Name: d.Get("name").(string),
SourceType: d.Get("source_type").(string),
Description: d.Get("description").(string),
Expand All @@ -82,7 +103,15 @@ func resourceHTTPSourceCreate(d *schema.ResourceData, m interface{}) error {
MultilineProcessingEnabled: d.Get("multiline_processing_enabled").(bool),
UseAutolineMatching: d.Get("use_autoline_matching").(bool),
ManualPrefixRegexp: d.Get("manual_prefix_regexp").(string),
})
Filters: make([]sumologic.Filter, 0),
}

sumologicFilters := d.Get("filter").(*schema.Set).List()

s.Filters = appendFilters(sumologicFilters)

source, err := client.CreateHTTPSource(d.Get("collector_id").(int), s)

if err != nil {
return err
}
Expand Down Expand Up @@ -115,6 +144,7 @@ func resourceHTTPSourceRead(d *schema.ResourceData, m interface{}) error {
d.Set("use_autoline_matching", source.UseAutolineMatching)
d.Set("manual_prefix_regexp", source.ManualPrefixRegexp)
d.Set("url", source.Url)
d.Set("filter", source.Filters)

return nil
}
Expand All @@ -134,8 +164,13 @@ func resourceHTTPSourceUpdate(d *schema.ResourceData, m interface{}) error {
MultilineProcessingEnabled: d.Get("multiline_processing_enabled").(bool),
UseAutolineMatching: d.Get("use_autoline_matching").(bool),
ManualPrefixRegexp: d.Get("manual_prefix_regexp").(string),
Filters: make([]sumologic.Filter, 0),
}

sumologicFilters := d.Get("filter").(*schema.Set).List()

source.Filters = appendFilters(sumologicFilters)

_, etag, _ := client.GetHTTPSource(d.Get("collector_id").(int), id)

updatedSource, err := client.UpdateHTTPSource(d.Get("collector_id").(int), source, etag)
Expand Down
11 changes: 11 additions & 0 deletions sumologic/resource_sumologic_http_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ resource "sumologic_http_source" "source" {
name = "test"
collector_id = "${sumologic_hosted_collector.collector.id}"
source_type = "HTTP"
filter {
filter_type = "Exclude"
name = "No INFO"
regexp = "(?s).*\\[INFO\\].*(?s)"
}
filter {
filter_type = "Exclude"
name = "No DEBUG"
regexp = "(?s).*\\[DEBUG\\].*(?s)"
}
}
`, r)
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions vendor/github.com/nextgenhealthcare/sumologic-sdk-go/go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7a2dac8

Please sign in to comment.