Skip to content

Commit

Permalink
SUMO-245309 Fix UT related with tag filters
Browse files Browse the repository at this point in the history
  • Loading branch information
yuting-liu committed Jan 13, 2025
1 parent d747310 commit 0203590
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 94 deletions.
2 changes: 1 addition & 1 deletion sumologic/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func Provider() terraform.ResourceProvider {
"sumologic_rum_source": resourceSumologicRumSource(),
"sumologic_role_v2": resourceSumologicRoleV2(),
"sumologic_azure_event_hub_log_source": resourceSumologicGenericPollingSource(),
"sumologic_azure_metric_source": resourceSumologicGenericPollingSource(),
"sumologic_azure_metrics_source": resourceSumologicGenericPollingSource(),
},
DataSourcesMap: map[string]*schema.Resource{
"sumologic_cse_log_mapping_vendor_product": dataSourceCSELogMappingVendorAndProduct(),
Expand Down
135 changes: 65 additions & 70 deletions sumologic/resource_sumologic_generic_polling_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func resourceSumologicGenericPollingSource() *schema.Resource {
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{"AwsS3Bucket", "AwsElbBucket", "AwsCloudFrontBucket",
"AwsCloudTrailBucket", "AwsS3AuditBucket", "AwsCloudWatch", "AwsInventory", "AwsXRay", "GcpMetrics", "AwsS3ArchiveBucket", "AzureEventHubLog"}, false),
"AwsCloudTrailBucket", "AwsS3AuditBucket", "AwsCloudWatch", "AwsInventory", "AwsXRay", "GcpMetrics", "AwsS3ArchiveBucket", "AzureEventHubLog", "AzureMetrics"}, false),
}
pollingSource.Schema["scan_interval"] = &schema.Schema{
Type: schema.TypeInt,
Expand Down Expand Up @@ -226,7 +226,7 @@ func resourceSumologicGenericPollingSource() *schema.Resource {
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Optional: true,
Required: true,
},
"namespace": {
Type: schema.TypeString,
Expand Down Expand Up @@ -499,13 +499,16 @@ func getCustomServices(path map[string]interface{}) []string {

func flattenPollingTagFilters(v []interface{}) []map[string]interface{} {
var filters []map[string]interface{}
for _, d := range v {
switch t := d.(type) {
case TagFilter:
filter := v[0].(map[string]interface{})
switch filterType := filter["type"].(string); filterType {
case "AzureTagFilters": // do nothing
default:
for _, d := range v {
rawFilter := d.(map[string]interface{})
filter := map[string]interface{}{
"type": t.Type,
"namespace": t.Namespace,
"tags": t.Tags,
"type": rawFilter["type"].(string),
"namespace": rawFilter["namespace"].(string),
"tags": rawFilter["tags"].([]interface{}),
}
filters = append(filters, filter)
}
Expand All @@ -515,98 +518,88 @@ func flattenPollingTagFilters(v []interface{}) []map[string]interface{} {

func flattenPollingAzureTagFilters(v []interface{}) []map[string]interface{} {
var filters []map[string]interface{}
for _, d := range v {
switch t := d.(type) {
case AzureTagFilter:
filter := v[0].(map[string]interface{})
switch filterType := filter["type"].(string); filterType {
case "AzureTagFilters":
for _, d := range v {
rawFilter := d.(map[string]interface{})
filter := map[string]interface{}{
"type": t.Type,
"namespace": t.Namespace,
"tags": flattenAzureTagKeyValuePair(t.Tags),
"type": rawFilter["type"].(string),
"namespace": rawFilter["namespace"].(string),
"tags": flattenAzureTagKeyValuePair(rawFilter["tags"].([]interface{})),
}
filters = append(filters, filter)
}
default: // do nothing
}
return filters
}

func flattenAzureTagKeyValuePair(v []AzureTagKeyValuePair) []map[string]interface{} {
func flattenAzureTagKeyValuePair(v []interface{}) []map[string]interface{} {
var tags []map[string]interface{}
for _, d := range v {
log.Printf("I want to first know what is d and what I can cast it to %s", d)
tag := map[string]interface{}{
"name": d.Name,
"values": d.Values,
//"name": d.Name,
//"values": d.Values,
}
tags = append(tags, tag)
}
return tags
}

func getTagFilter(config map[string]interface{}) TagFilter {
filter := TagFilter{}
filter.Type = config["type"].(string)
filter.Namespace = config["namespace"].(string)
rawTags := config["tags"].([]interface{})
Tags := make([]string, len(rawTags))
for i, v := range rawTags {
Tags[i] = v.(string)
}
filter.Tags = Tags
return filter
}
func getPollingTagFilters(d *schema.ResourceData) []interface{} {
paths := d.Get("path").([]interface{})
path := paths[0].(map[string]interface{})
rawTagFilterConfig := path["tag_filters"].([]interface{})
var filters []interface{}

for _, rawConfig := range rawTagFilterConfig {
config := rawConfig.(map[string]interface{})
filter := TagFilter{}
filter.Type = config["type"].(string)
filter.Namespace = config["namespace"].(string)

func getAzureTagFilter(rawTags []interface{}, filterType string, filterNamespace string) AzureTagFilter {
filter := AzureTagFilter{}
filter.Type = filterType
filter.Namespace = filterNamespace
Tags := make([]AzureTagKeyValuePair, len(rawTags))
for i, rawTagPair := range rawTags {
tagPair := rawTagPair.(map[string]interface{})
pair := AzureTagKeyValuePair{}
pair.Name = tagPair["name"].(string)
// get the values from the azure key value pair
rawValues := tagPair["values"].([]interface{})
valueList := make([]string, len(rawValues))
for j, v := range rawValues {
valueList[j] = v.(string)
rawTags := config["tags"].([]interface{})
Tags := make([]string, len(rawTags))
for i, v := range rawTags {
Tags[i] = v.(string)
}
pair.Values = valueList
Tags[i] = pair
filter.Tags = Tags
filters = append(filters, filter)
}
return filter
return filters
}

func getPollingTagFilters(d *schema.ResourceData) []interface{} {
func getPollingAzureTagFilters(d *schema.ResourceData) []interface{} {
paths := d.Get("path").([]interface{})
path := paths[0].(map[string]interface{})
rawTagFilterConfig := path["tag_filters"].([]interface{})
rawTagFilterConfig := path["azure_tag_filters"].([]interface{})
var filters []interface{}

for _, rawConfig := range rawTagFilterConfig {
config := rawConfig.(map[string]interface{})
filterType := config["type"].(string)
filterNamespace := config["namespace"].(string)
rawTags := config["tags"].([]interface{})
filter := AzureTagFilter{}
filter.Type = config["type"].(string)
filter.Namespace = config["namespace"].(string)

switch filterType {
case "TagFilters":
filter := getTagFilter(config)
filters = append(filters, filter)
case "AzureTagFilters":
filter := getAzureTagFilter(rawTags, filterType, filterNamespace)
filters = append(filters, filter)
// type is optional
default:
if len(rawTags) > 0 {
switch rawTags[0].(type) {
case map[string]interface{}:
filter := getAzureTagFilter(rawTags, filterType, filterNamespace)
filters = append(filters, filter)
case string:
filter := getTagFilter(config)
filters = append(filters, filter)
}
rawTags := config["tags"].([]interface{})
Tags := make([]AzureTagKeyValuePair, len(rawTags))
for i, rawTagPair := range rawTags {
tagPair := rawTagPair.(map[string]interface{})
pair := AzureTagKeyValuePair{}
pair.Name = tagPair["name"].(string)
// get the values from the azure key value pair
rawValues := tagPair["values"].([]interface{})
valueList := make([]string, len(rawValues))
for j, v := range rawValues {
valueList[j] = v.(string)
}
pair.Values = valueList
Tags[i] = pair
}
filter.Tags = Tags
filters = append(filters, filter)
}
return filters
}
Expand Down Expand Up @@ -701,6 +694,7 @@ func getPollingAuthentication(d *schema.ResourceData) (PollingAuthentication, er
authSettings.SharedAccessPolicyName = auth["shared_access_policy_name"].(string)
authSettings.SharedAccessPolicyKey = auth["shared_access_policy_key"].(string)
case "AzureClientSecretAuthentication":
authSettings.Type = "AzureClientSecretAuthentication"
authSettings.TenantId = auth["tenant_id"].(string)
authSettings.ClientId = auth["client_id"].(string)
authSettings.ClientSecret = auth["client_secret"].(string)
Expand Down Expand Up @@ -807,7 +801,8 @@ func getPollingPathSettings(d *schema.ResourceData) (PollingPath, error) {
LimitToNamespaces = append(LimitToNamespaces, v.(string))
}
}
pathSettings.TagFilters = getPollingTagFilters(d)
pathSettings.LimitToNamespaces = LimitToNamespaces
pathSettings.TagFilters = getPollingAzureTagFilters(d)
default:
errorMessage := fmt.Sprintf("[ERROR] Unknown resourceType in path: %v", pathType)
log.Print(errorMessage)
Expand Down
33 changes: 10 additions & 23 deletions sumologic/resource_sumologic_polling_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func resourceSumologicPollingSource() *schema.Resource {
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Optional: true,
Required: true,
},
"namespace": {
Type: schema.TypeString,
Expand Down Expand Up @@ -355,30 +355,17 @@ func getTagFilters(d *schema.ResourceData) []interface{} {

for _, rawConfig := range rawTagFilterConfig {
config := rawConfig.(map[string]interface{})
filterType := config["type"].(string)
filterNamespace := config["namespace"].(string)
rawTags := config["tags"].([]interface{})
filter := TagFilter{}
filter.Type = config["type"].(string)
filter.Namespace = config["namespace"].(string)

switch filterType {
case "TagFilters":
filter := getTagFilter(config)
filters = append(filters, filter)
case "AzureTagFilters":
filter := getAzureTagFilter(rawTags, filterType, filterNamespace)
filters = append(filters, filter)
// type is optional
default:
if len(rawTags) > 0 {
switch rawTags[0].(type) {
case map[string]interface{}:
filter := getAzureTagFilter(rawTags, filterType, filterNamespace)
filters = append(filters, filter)
case string:
filter := getTagFilter(config)
filters = append(filters, filter)
}
}
rawTags := config["tags"].([]interface{})
Tags := make([]string, len(rawTags))
for i, v := range rawTags {
Tags[i] = v.(string)
}
filter.Tags = Tags
filters = append(filters, filter)
}
return filters
}
Expand Down

0 comments on commit 0203590

Please sign in to comment.