Skip to content

Commit

Permalink
feat: partitions data sources
Browse files Browse the repository at this point in the history
  • Loading branch information
imranismail committed Mar 21, 2024
1 parent 9891e44 commit 021200c
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 2 deletions.
4 changes: 2 additions & 2 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ install: fmtcheck
install-dev: fmtcheck
mkdir -vp $(PLUGIN_DIR)
ifeq ($(UNAME), arm64)
go build ${DEBUG_FLAGS} -o $(PLUGIN_DIR)/sumologic.com/dev/sumologic/1.0.0/darwin_arm64/terraform-provider-sumologic
go build ${DEBUG_FLAGS} -o $(PLUGIN_DIR)/sumologic.com/dev/sumologic/1.0.0/darwin_arm64/terraform-provider-sumologic
else
go build ${DEBUG_FLAGS} -o $(PLUGIN_DIR)/sumologic.com/dev/sumologic/1.0.0/darwin_amd64/terraform-provider-sumologic
endif
Expand All @@ -49,7 +49,7 @@ fmtcheck:
lint:
@echo "==> Checking source code against linters..."
golangci-lint run ./...

test: fmtcheck
go test -i $(TEST) || exit 1
echo $(TEST) | \
Expand Down
77 changes: 77 additions & 0 deletions sumologic/data_source_sumologic_partition.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package sumologic

import (
"fmt"

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

func dataSourceSumologicPartition() *schema.Resource {
return &schema.Resource{
Read: dataSourceSumologicPartitionRead,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
},
"routing_expression": {
Type: schema.TypeString,
},
"analytics_tier": {
Type: schema.TypeString,
},
"retention_period": {
Type: schema.TypeInt,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return new == "-1" && old != ""
},
},
"is_compliant": {
Type: schema.TypeBool,
},
"total_bytes": {
Type: schema.TypeInt,
},
"data_forwarding_id": {
Type: schema.TypeString,
},
"is_active": {
Type: schema.TypeBool,
},
"index_type": {
Type: schema.TypeString,
},
"reduce_retention_period_immediately": {
Type: schema.TypeBool,
},
},
}
}

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

id := d.Id()
spartition, err := c.GetPartition(id)

if err != nil {
return fmt.Errorf("error retrieving partition: %v", err)
}

if spartition == nil {
d.SetId("")
return nil
}

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
}
40 changes: 40 additions & 0 deletions sumologic/data_source_sumologic_partitions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package sumologic

import (
"fmt"

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

func dataSourceSumologicPartitions() *schema.Resource {
return &schema.Resource{
Read: dataSourceSumologicPartitionsRead,

Schema: map[string]*schema.Schema{
"ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}

func dataSourceSumologicPartitionsRead(d *schema.ResourceData, meta interface{}) error {
c := meta.(*Client)
spartitions, err := c.ListPartitions()
if err != nil {
return fmt.Errorf("error retrieving partitions: %v", err)
}

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

for _, partition := range spartitions {
ids = append(ids, partition.ID)
}

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

return nil
}
2 changes: 2 additions & 0 deletions sumologic/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ func Provider() terraform.ResourceProvider {
"sumologic_my_user_id": dataSourceSumologicMyUserId(),
"sumologic_role": dataSourceSumologicRole(),
"sumologic_user": dataSourceSumologicUser(),
"sumologic_partitions": dataSourceSumologicPartitions(),
"sumologic_partition": dataSourceSumologicPartition(),
},
ConfigureFunc: providerConfigure,
}
Expand Down
15 changes: 15 additions & 0 deletions sumologic/sumologic_partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@ import (
"strings"
)

func (s *Client) ListPartitions() ([]Partition, error) {
data, _, err := s.Get("v1/partitions")
if err != nil {
return nil, err
}

var spartitions []Partition
err = json.Unmarshal(data, &spartitions)
if err != nil {
return nil, err
}

return spartitions, nil
}

func (s *Client) GetPartition(id string) (*Partition, error) {
data, _, err := s.Get(fmt.Sprintf("v1/partitions/%s", id))
if err != nil {
Expand Down

0 comments on commit 021200c

Please sign in to comment.