Skip to content

Commit 0f9fd53

Browse files
authored
Merge pull request #762 from SumoLogic/erikatsumo-datasource-partition
New data sources for partition
2 parents ad9222d + 19f05d2 commit 0f9fd53

11 files changed

+382
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
## X.Y.Z (Unreleased)
22
* Add new change notes here
33

4+
FEATURES:
5+
* **New Data Source:** sumologic_partition
6+
* **New Data Source:** sumologic_partitions
7+
48
ENHANCEMENTS:
59
* Add support for searchable time for dashboard field time_source
610

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package sumologic
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
7+
)
8+
9+
func dataSourceSumologicPartition() *schema.Resource {
10+
return &schema.Resource{
11+
Read: dataSourceSumologicPartitionRead,
12+
Schema: dataSourcePartitionSchema(),
13+
}
14+
}
15+
16+
func dataSourcePartitionSchema() map[string]*schema.Schema {
17+
return map[string]*schema.Schema{
18+
"id": {
19+
Type: schema.TypeString,
20+
Required: true,
21+
},
22+
"name": {
23+
Type: schema.TypeString,
24+
Computed: true,
25+
},
26+
"routing_expression": {
27+
Type: schema.TypeString,
28+
Computed: true,
29+
},
30+
"analytics_tier": {
31+
Type: schema.TypeString,
32+
Computed: true,
33+
},
34+
"retention_period": {
35+
Type: schema.TypeInt,
36+
Computed: true,
37+
},
38+
"is_compliant": {
39+
Type: schema.TypeBool,
40+
Computed: true,
41+
},
42+
"total_bytes": {
43+
Type: schema.TypeInt,
44+
Computed: true,
45+
},
46+
"data_forwarding_id": {
47+
Type: schema.TypeString,
48+
Computed: true,
49+
},
50+
"is_active": {
51+
Type: schema.TypeBool,
52+
Computed: true,
53+
},
54+
"index_type": {
55+
Type: schema.TypeString,
56+
Computed: true,
57+
},
58+
"reduce_retention_period_immediately": {
59+
Type: schema.TypeBool,
60+
Computed: true,
61+
},
62+
"is_included_in_default_search": {
63+
Type: schema.TypeBool,
64+
Computed: true,
65+
},
66+
}
67+
}
68+
69+
func dataSourceSumologicPartitionRead(d *schema.ResourceData, meta interface{}) error {
70+
c := meta.(*Client)
71+
72+
var err error
73+
var spartition *Partition
74+
75+
rid, ok := d.GetOk("id")
76+
77+
if !ok {
78+
return fmt.Errorf("please specify the partition id")
79+
}
80+
81+
id := rid.(string)
82+
spartition, err = c.GetPartition(id)
83+
if err != nil {
84+
return fmt.Errorf("error retrieving partition with id %v: %v", id, err)
85+
}
86+
if spartition == nil {
87+
return fmt.Errorf("partition with id %v not found", id)
88+
}
89+
90+
d.SetId(spartition.ID)
91+
d.Set("routing_expression", spartition.RoutingExpression)
92+
d.Set("name", spartition.Name)
93+
d.Set("analytics_tier", spartition.AnalyticsTier)
94+
d.Set("retention_period", spartition.RetentionPeriod)
95+
d.Set("is_compliant", spartition.IsCompliant)
96+
d.Set("data_forwarding_id", spartition.DataForwardingId)
97+
d.Set("is_active", spartition.IsActive)
98+
d.Set("total_bytes", spartition.TotalBytes)
99+
d.Set("index_type", spartition.IndexType)
100+
d.Set("reduce_retention_period_immediately", spartition.ReduceRetentionPeriodImmediately)
101+
d.Set("is_included_in_default_search", spartition.IsIncludedInDefaultSearch)
102+
103+
return nil
104+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package sumologic
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
7+
)
8+
9+
func TestAccDataSourcePartition_basic(t *testing.T) {
10+
resource.Test(t, resource.TestCase{
11+
PreCheck: func() { testAccPreCheck(t) },
12+
Providers: testAccProviders,
13+
Steps: []resource.TestStep{
14+
{
15+
Config: testDataSourceSumologicPartitionConfig,
16+
Check: resource.ComposeTestCheckFunc(
17+
testAccDataSourcePartitionCheck("data.sumologic_partition.test"),
18+
),
19+
},
20+
},
21+
})
22+
}
23+
24+
func testAccDataSourcePartitionCheck(name string) resource.TestCheckFunc {
25+
return resource.ComposeTestCheckFunc(
26+
resource.TestCheckResourceAttr(name, "id", "00000000000AD88D"),
27+
resource.TestCheckResourceAttr(name, "name", "apache"),
28+
resource.TestCheckResourceAttr(name, "routing_expression", "_sourcecategory=*/Apache"),
29+
resource.TestCheckResourceAttr(name, "analytics_tier", "continuous"),
30+
resource.TestCheckResourceAttr(name, "retention_period", "365"),
31+
resource.TestCheckResourceAttr(name, "is_compliant", "false"),
32+
resource.TestCheckResourceAttr(name, "is_active", "true"),
33+
resource.TestCheckResourceAttr(name, "total_bytes", "0"),
34+
resource.TestCheckResourceAttr(name, "data_forwarding_id", ""),
35+
resource.TestCheckResourceAttr(name, "index_type", "Partition"),
36+
resource.TestCheckResourceAttr(name, "reduce_retention_period_immediately", "false"),
37+
resource.TestCheckResourceAttr(name, "is_included_in_default_search", "true"),
38+
)
39+
}
40+
41+
var testDataSourceSumologicPartitionConfig = `
42+
data "sumologic_partition" "test" {
43+
id = "00000000000AD88D"
44+
}
45+
`
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package sumologic
2+
3+
import (
4+
"crypto/sha256"
5+
"encoding/hex"
6+
"fmt"
7+
"sort"
8+
"strings"
9+
10+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
11+
)
12+
13+
func dataSourceSumologicPartitions() *schema.Resource {
14+
return &schema.Resource{
15+
Read: dataSourceSumologicPartitionsRead,
16+
17+
Schema: map[string]*schema.Schema{
18+
"partitions": {
19+
Type: schema.TypeList,
20+
Computed: true,
21+
Elem: &schema.Resource{
22+
Schema: dataSourcePartitionSchema(),
23+
Read: dataSourceSumologicPartitionRead,
24+
},
25+
},
26+
},
27+
}
28+
}
29+
30+
func dataSourceSumologicPartitionsRead(d *schema.ResourceData, meta interface{}) error {
31+
c := meta.(*Client)
32+
33+
spartitions, err := c.ListPartitions()
34+
if err != nil {
35+
return fmt.Errorf("error retrieving partitions: %v", err)
36+
}
37+
38+
partitions := make([]map[string]interface{}, 0, len(spartitions))
39+
40+
for _, spartition := range spartitions {
41+
partition := map[string]interface{}{
42+
"id": spartition.ID,
43+
"name": spartition.Name,
44+
"routing_expression": spartition.RoutingExpression,
45+
"analytics_tier": spartition.AnalyticsTier,
46+
"retention_period": spartition.RetentionPeriod,
47+
"is_compliant": spartition.IsCompliant,
48+
"total_bytes": spartition.TotalBytes,
49+
"data_forwarding_id": spartition.DataForwardingId,
50+
"is_active": spartition.IsActive,
51+
"index_type": spartition.IndexType,
52+
"is_included_in_default_search": spartition.IsIncludedInDefaultSearch,
53+
}
54+
55+
partitions = append(partitions, partition)
56+
}
57+
58+
d.Set("partitions", partitions)
59+
d.SetId(generatePartitionsId(spartitions))
60+
61+
return nil
62+
}
63+
64+
func generatePartitionsId(partitions []Partition) string {
65+
// Collect a sorted list of partition IDs
66+
ids := []string{"partition_ids"}
67+
for _, partition := range partitions {
68+
ids = append(ids, partition.ID)
69+
}
70+
sort.Strings(ids)
71+
72+
// Join all IDs and create a hash
73+
idString := strings.Join(ids, "|")
74+
hash := sha256.Sum256([]byte(idString))
75+
return hex.EncodeToString(hash[:])
76+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package sumologic
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
7+
)
8+
9+
func TestAccDataSourcePartitions_basic(t *testing.T) {
10+
resource.Test(t, resource.TestCase{
11+
PreCheck: func() { testAccPreCheck(t) },
12+
Providers: testAccProviders,
13+
Steps: []resource.TestStep{
14+
{
15+
Config: testDataSourceSumologicPartitionsConfig,
16+
Check: resource.ComposeTestCheckFunc(
17+
checkResourceAttrGreaterThanZero("data.sumologic_partitions.test", "partitions.#"),
18+
),
19+
},
20+
},
21+
})
22+
}
23+
24+
var testDataSourceSumologicPartitionsConfig = `
25+
data "sumologic_partitions" "test" {}
26+
`

sumologic/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ func Provider() terraform.ResourceProvider {
134134
"sumologic_personal_folder": dataSourceSumologicPersonalFolder(),
135135
"sumologic_folder": dataSourceSumologicFolder(),
136136
"sumologic_my_user_id": dataSourceSumologicMyUserId(),
137+
"sumologic_partition": dataSourceSumologicPartition(),
138+
"sumologic_partitions": dataSourceSumologicPartitions(),
137139
"sumologic_role": dataSourceSumologicRole(),
138140
"sumologic_role_v2": dataSourceSumologicRoleV2(),
139141
"sumologic_user": dataSourceSumologicUser(),

sumologic/resource_sumologic_cse_rule_tuning_expression_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func TestAccSumologicSCERuleTuningExpression_create(t *testing.T) {
1717
var ruleTuningExpression CSERuleTuningExpression
1818
nName := acctest.RandomWithPrefix("New Rule Tuning Name")
1919
nDescription := "New Rule Tuning Description"
20-
nExpression := "expression"
20+
nExpression := "accountId = 1234"
2121
nEnabled := true
2222
nExclude := true
2323
nIsGlobal := false

sumologic/sumologic_partition.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,47 @@ func (s *Client) UpdatePartition(spartition Partition) error {
6363
return err
6464
}
6565

66+
func (s *Client) ListPartitions() ([]Partition, error) {
67+
var listPartitionResp ListPartitionResp
68+
69+
data, err := s.Get("v1/partitions")
70+
if err != nil {
71+
return nil, err
72+
}
73+
74+
err = json.Unmarshal(data, &listPartitionResp)
75+
if err != nil {
76+
return nil, err
77+
}
78+
79+
spartitions := listPartitionResp.Data
80+
81+
for listPartitionResp.Next != "" {
82+
data, err = s.Get("v1/partitions?token=" + listPartitionResp.Next)
83+
if err != nil {
84+
return nil, err
85+
}
86+
87+
listPartitionResp.Reset()
88+
89+
err = json.Unmarshal(data, &listPartitionResp)
90+
if err != nil {
91+
return nil, err
92+
}
93+
94+
spartitions = append(spartitions, listPartitionResp.Data...)
95+
}
96+
97+
var activePartitions []Partition
98+
for _, partition := range spartitions {
99+
if partition.IsActive {
100+
activePartitions = append(activePartitions, partition)
101+
}
102+
}
103+
104+
return activePartitions, nil
105+
}
106+
66107
type Partition struct {
67108
ID string `json:"id,omitempty"`
68109
Name string `json:"name"`
@@ -77,3 +118,13 @@ type Partition struct {
77118
ReduceRetentionPeriodImmediately bool `json:"reduceRetentionPeriodImmediately,omitempty"`
78119
IsIncludedInDefaultSearch *bool `json:"isIncludedInDefaultSearch,omitempty"`
79120
}
121+
122+
type ListPartitionResp struct {
123+
Data []Partition `json:"data"`
124+
Next string `json:"next"`
125+
}
126+
127+
func (s *ListPartitionResp) Reset() {
128+
s.Data = nil
129+
s.Next = ""
130+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
layout: "sumologic"
3+
page_title: "SumoLogic: sumologic_partition"
4+
description: |-
5+
Provides a way to retrieve Sumo Logic partition details (id, name, etc) for a partition managed outside of terraform.
6+
---
7+
8+
# sumologic_partition
9+
10+
Provides a way to retrieve a [Sumologic Partition][1]'s details (id, name, etc) for a partition
11+
managed by another terraform stack.
12+
13+
14+
## Example Usage
15+
16+
```hcl
17+
data "sumologic_partition" "this" {
18+
id = "1234567890"
19+
}
20+
```
21+
22+
A partition can be looked up by `id`.
23+
24+
## Attributes reference
25+
26+
The following attributes are exported:
27+
28+
- `name` - The name of the partition.
29+
- `routing_expression` - The query that defines the data to be included in the partition.
30+
- `analytics_tier` - The Data Tier where the data in the partition will reside. Possible values are: `continuous`, `frequent`, `infrequent`
31+
- `retention_period` - The number of days to retain data in the partition.
32+
- `is_compliant` - Whether the partition is used for compliance or audit purposes.
33+
- `is_included_in_default_search` - Whether the partition is included in the default search scope.
34+
- `total_bytes` - The size of the data in the partition in bytes.
35+
- `is_active` - Whether the partition is currently active or decommissioned.
36+
- `index_type` - The type of partition index. Possible values are: `DefaultIndex`, `AuditIndex`or `Partition`
37+
- `data_forwarding_id` - The ID of the data forwarding configuration to be used by the partition.
38+
39+
[1]: https://help.sumologic.com/docs/manage/partitions/data-tiers/

0 commit comments

Comments
 (0)