Skip to content

Commit

Permalink
chore: support declaring data forwarding rule
Browse files Browse the repository at this point in the history
  • Loading branch information
imranismail committed Mar 22, 2024
1 parent 02001ee commit b879ec0
Show file tree
Hide file tree
Showing 8 changed files with 373 additions and 12 deletions.
84 changes: 81 additions & 3 deletions sumologic/data_source_sumologic_partition.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,91 @@
package sumologic

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func dataSourceSumologicPartition() *schema.Resource {
resource := resourceSumologicPartition()
return &schema.Resource{
Read: resource.Read,
Schema: resource.Schema,
Read: dataSourceSumologicPartitionRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"routing_expression": {
Type: schema.TypeString,
Computed: true,
},
"analytics_tier": {
Type: schema.TypeString,
Computed: true,
},
"retention_period": {
Type: schema.TypeInt,
Computed: true,
},
"is_compliant": {
Type: schema.TypeBool,
Computed: true,
},
"total_bytes": {
Type: schema.TypeInt,
Computed: true,
},
"data_forwarding_id": {
Type: schema.TypeString,
Computed: true,
},
"is_active": {
Type: schema.TypeBool,
Computed: true,
},
"index_type": {
Type: schema.TypeString,
Computed: true,
},
"reduce_retention_period_immediately": {
Type: schema.TypeBool,
Optional: true,
},
},
}
}

func dataSourceSumologicPartitionRead(d *schema.ResourceData, meta interface{}) error {
c := meta.(*Client)

var err error
var spartition *Partition

if rid, ok := d.GetOk("id"); ok {
id := rid.(string)
spartition, err = c.GetPartition(id)
if err != nil {
return fmt.Errorf("partition with id %v not found: %v", id, err)
}
if spartition == nil {
return fmt.Errorf("partition with id %v not found", id)
}
}

d.SetId(spartition.ID)
d.Set("routing_expression", spartition.RoutingExpression)
d.Set("name", spartition.Name)
d.Set("analytics_tier", spartition.AnalyticsTier)
d.Set("retention_period", spartition.RetentionPeriod)
d.Set("is_compliant", spartition.IsCompliant)
d.Set("data_forwarding_id", spartition.DataForwardingId)
d.Set("is_active", spartition.IsActive)
d.Set("total_bytes", spartition.TotalBytes)
d.Set("index_type", spartition.IndexType)

return nil
}
81 changes: 78 additions & 3 deletions sumologic/data_source_sumologic_partitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package sumologic

import (
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)

func dataSourceSumologicPartitions() *schema.Resource {
Expand All @@ -12,10 +14,33 @@ func dataSourceSumologicPartitions() *schema.Resource {

Schema: map[string]*schema.Schema{
"ids": {
Type: schema.TypeList,
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"filters": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{"name", "routingExpression", "id"}, false),
},
"operator": {
Type: schema.TypeString,
Optional: true,
Default: "equals",
ValidateFunc: validation.StringInSlice([]string{"Equals", "Contains", "HasPrefix", "HasSuffix"}, false),
},
"value": {
Type: schema.TypeString,
Required: true,
},
},
},
},
},
}
}
Expand All @@ -27,14 +52,64 @@ func dataSourceSumologicPartitionsRead(d *schema.ResourceData, meta interface{})
return fmt.Errorf("error retrieving partitions: %v", err)
}

ids := make([]string, 0, len(spartitions))
ids := []string{}

filters, ok := d.GetOk("filters")
if !ok {
return fmt.Errorf("no filters provided")
}

for _, partition := range spartitions {
ids = append(ids, partition.ID)
if matchesAllFilters(partition, filters.([]interface{})) {
ids = append(ids, partition.ID)
}
}

d.SetId(c.BaseURL.Host)
d.Set("ids", ids)

return nil
}

func matchesAllFilters(partition Partition, filters []interface{}) bool {
for _, filter := range filters {
f := filter.(map[string]interface{})
k := f["key"].(string)
o := f["operator"].(string)
v := f["value"].(string)

var fv string
switch k {
case "name":
fv = partition.Name
case "routingExpression":
fv = partition.RoutingExpression
case "id":
fv = partition.ID
default:
return false
}

switch o {
case "Equals":
if fv != v {
return false
}
case "Contains":
if !strings.Contains(fv, v) {
return false
}
case "HasPrefix":
if !strings.HasPrefix(fv, v) {
return false
}
case "HasSuffix":
if !strings.HasSuffix(fv, v) {
return false
}
default:
return false
}
}
return true
}
1 change: 1 addition & 0 deletions sumologic/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func Provider() terraform.ResourceProvider {
"sumologic_s3_audit_source": resourceSumologicGenericPollingSource(),
"sumologic_s3_archive_source": resourceSumologicGenericPollingSource(),
"sumologic_s3_data_forwarding_destination": resourceSumologicS3DataForwardingDestination(),
"sumologic_s3_data_forwarding_rule": resourceSumologicS3DataForwardingRule(),
"sumologic_cloudwatch_source": resourceSumologicGenericPollingSource(),
"sumologic_aws_inventory_source": resourceSumologicGenericPollingSource(),
"sumologic_aws_xray_source": resourceSumologicGenericPollingSource(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ func resourceSumologicS3DataForwardingDestination() *schema.Resource {
"encrypted": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"bucket_name": {
Type: schema.TypeString,
Expand Down Expand Up @@ -120,6 +122,7 @@ func resourceSumologicS3DataForwardingDestinationDelete(d *schema.ResourceData,

func resourceToS3DataForwardingDestination(d *schema.ResourceData) S3DataForwardingDestination {
return S3DataForwardingDestination{
ID: d.Id(),
Name: d.Get("name").(string),
Description: d.Get("description").(string),
AuthenticationMode: d.Get("authentication_mode").(string),
Expand Down
104 changes: 104 additions & 0 deletions sumologic/resource_sumologic_s3_data_forwarding_rule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package sumologic

import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func resourceSumologicS3DataForwardingRule() *schema.Resource {
return &schema.Resource{
Create: resourceSumologicS3DataForwardingRuleCreate,
Read: resourceSumologicS3DataForwardingRuleRead,
Update: resourceSumologicS3DataForwardingRuleUpdate,
Delete: resourceSumologicS3DataForwardingRuleDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"index_id": {
Type: schema.TypeString,
Required: true,
},
"destination_id": {
Type: schema.TypeString,
Required: true,
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"file_format": {
Type: schema.TypeString,
Optional: true,
Default: "{index}_{day}_{hour}_{minute}_{second}",
},
"format": {
Type: schema.TypeString,
Optional: true,
Default: "csv",
},
},
}
}

func resourceSumologicS3DataForwardingRuleCreate(d *schema.ResourceData, meta interface{}) error {
c := meta.(*Client)

if d.Id() == "" {
dfr := resourceToS3DataForwardingRule(d)
createdDfd, err := c.CreateS3DataForwardingRule(dfr)

if err != nil {
return err
}

d.SetId(createdDfd.ID)
}

return resourceSumologicS3DataForwardingRuleUpdate(d, meta)
}

func resourceSumologicS3DataForwardingRuleRead(d *schema.ResourceData, meta interface{}) error {
c := meta.(*Client)
dfr, err := c.GetS3DataForwardingRule(d.Id())

if err != nil {
return err
}

d.Set("index_id", dfr.IndexID)
d.Set("destination_id", dfr.DestinationID)
d.Set("enabled", dfr.Enabled)
d.Set("file_format", dfr.FileFormat)
d.Set("format", dfr.Format)

return nil
}

func resourceSumologicS3DataForwardingRuleUpdate(d *schema.ResourceData, meta interface{}) error {
c := meta.(*Client)
dfr := resourceToS3DataForwardingRule(d)
err := c.UpdateS3DataForwardingRule(dfr)

if err != nil {
return err
}

return resourceSumologicS3DataForwardingRuleRead(d, meta)
}

func resourceSumologicS3DataForwardingRuleDelete(d *schema.ResourceData, meta interface{}) error {
c := meta.(*Client)
return c.DeleteS3DataForwardingRule(d.Id())
}

func resourceToS3DataForwardingRule(d *schema.ResourceData) S3DataForwardingRule {
return S3DataForwardingRule{
ID: d.Id(),
IndexID: d.Get("index_id").(string),
DestinationID: d.Get("destination_id").(string),
Enabled: d.Get("enabled").(bool),
FileFormat: d.Get("file_format").(string),
Format: d.Get("format").(string),
}
}
Loading

0 comments on commit b879ec0

Please sign in to comment.