Skip to content

Commit 54faa54

Browse files
authored
Merge pull request #11 from benzaita/lx-2265-add-colselection-to-object-groups
LX-2265 Added ColSelection parameter to Object Group resource
2 parents 9860333 + fd36d49 commit 54faa54

File tree

9 files changed

+120
-59
lines changed

9 files changed

+120
-59
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ HOSTNAME=registry.terraform.io
33
NAMESPACE=benzaita
44
NAME=chaossearch
55
BINARY=terraform-provider-${NAME}
6-
VERSION=0.7.0
6+
VERSION=0.8.0
77
OS_ARCH=$(shell go env GOOS)_$(shell go env GOARCH)
88

99
default: install

chaossearch/client/configuration.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ type Configuration struct {
66
AccessKeyID string
77
SecretAccessKey string
88
AWSServiceName string
9-
Region string
9+
Region string
1010
}
1111

1212
// NewConfiguration creates a default Configuration struct

chaossearch/client/model.go

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,32 @@ type ReadObjectGroupRequest struct {
1818
}
1919

2020
type ReadObjectGroupResponse struct {
21-
Compression string
22-
FilterJSON string
23-
Format string
24-
Pattern string
25-
LiveEventsSqsArn string
26-
PartitionBy string
27-
SourceBucket string
28-
IndexRetention int
21+
Compression string
22+
FilterJSON string
23+
Format string
24+
Pattern string
25+
LiveEventsSqsArn string
26+
PartitionBy string
27+
SourceBucket string
28+
IndexRetention int
2929
ArrayFlattenDepth *int
30-
ColumnRenames map[string]string
30+
ColumnRenames map[string]string
31+
ColumnSelection []map[string]interface{}
3132
}
3233

3334
type CreateObjectGroupRequest struct {
34-
Name string
35-
Compression string
36-
FilterJSON string
37-
Format string
38-
LiveEventsSqsArn string
39-
PartitionBy string
40-
SourceBucket string
41-
Pattern string
42-
IndexRetention int
35+
Name string
36+
Compression string
37+
FilterJSON string
38+
Format string
39+
LiveEventsSqsArn string
40+
PartitionBy string
41+
SourceBucket string
42+
Pattern string
43+
IndexRetention int
4344
ArrayFlattenDepth *int
44-
ColumnRenames map[string]interface{}
45+
ColumnRenames map[string]interface{}
46+
ColumnSelection map[string]interface{}
4547
}
4648

4749
type UpdateIndexingStateRequest struct {

chaossearch/client/operation_create_object_group.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ func marshalCreateObjectGroupRequest(req *CreateObjectGroupRequest) ([]byte, err
3636
"bucket": req.Name,
3737
"source": req.SourceBucket,
3838
"format": map[string]interface{}{
39-
"_type": req.Format,
40-
"horizontal": true,
41-
"stripPrefix": true,
39+
"_type": req.Format,
40+
"horizontal": true,
41+
"stripPrefix": true,
4242
"arrayFlattenDepth": req.ArrayFlattenDepth,
4343
},
4444
"indexRetention": req.IndexRetention,
@@ -56,6 +56,24 @@ func marshalCreateObjectGroupRequest(req *CreateObjectGroupRequest) ([]byte, err
5656
options["colRenames"] = req.ColumnRenames
5757
}
5858

59+
if len(req.ColumnSelection) > 0 {
60+
var options = body["options"].(map[string]interface{})
61+
// @example
62+
//"colSelection": [
63+
// {
64+
// "includes": [
65+
// "orig._originalSource",
66+
// "attrs.version",
67+
// "line.message",
68+
// "line.correlation_id",
69+
// "Timestamp"
70+
// ],
71+
// "type": "whitelist"
72+
// }
73+
//],
74+
options["colSelection"] = []map[string]interface{}{req.ColumnSelection}
75+
}
76+
5977
if req.Compression != "" {
6078
var options = body["options"].(map[string]interface{})
6179
options["compression"] = req.Compression

chaossearch/client/operation_read_indexing_state.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"net/http"
99
)
1010

11-
1211
// There are other nested structures that we don't marshal here because we don't need to.
1312
// Feel free to add them in the future when needed
1413
type readBucketMetadataResponse struct {
@@ -21,7 +20,7 @@ func (client *Client) ReadIndexingState(ctx context.Context, req *ReadIndexingSt
2120
method := "POST"
2221
body := &readBucketMetadataRequest{
2322
BucketName: req.ObjectGroupName,
24-
Stats: false,
23+
Stats: false,
2524
}
2625

2726
response, err := makeGetBucketMetadataRequest(method, client, ctx, body)
@@ -31,7 +30,7 @@ func (client *Client) ReadIndexingState(ctx context.Context, req *ReadIndexingSt
3130

3231
bucketMetadata := &IndexingState{
3332
ObjectGroupName: response.Bucket,
34-
Active: false,
33+
Active: false,
3534
}
3635

3736
if response.State == "Active" || response.State == "Idle" {
@@ -60,12 +59,11 @@ func makeGetBucketMetadataRequest(method string, client *Client, ctx context.Con
6059
}
6160
defer httpResp.Body.Close()
6261

63-
6462
var response readBucketMetadataResponse
6563
err = client.unmarshalJSONBody(httpResp.Body, &response)
6664
if err != nil {
6765
return nil, fmt.Errorf("failed to unmarshal response: %s", err)
6866
}
6967

7068
return &response, nil
71-
}
69+
}

chaossearch/client/operation_read_object_group.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ func (client *Client) readAttributesFromDatasetEndpoint(ctx context.Context, req
5353
var getDatasetResp struct {
5454
PartitionBy string `json:"partitionBy"`
5555
Options struct {
56-
ColumnRenames map[string]string `json:"colRenames"`
56+
ColumnRenames map[string]string `json:"colRenames"`
57+
ColumnSelection []map[string]interface{} `json:"colSelection"`
5758
} `json:"options"`
5859
}
5960
if err := client.unmarshalJSONBody(httpResp.Body, &getDatasetResp); err != nil {
@@ -62,6 +63,7 @@ func (client *Client) readAttributesFromDatasetEndpoint(ctx context.Context, req
6263

6364
resp.PartitionBy = getDatasetResp.PartitionBy
6465
resp.ColumnRenames = getDatasetResp.Options.ColumnRenames
66+
resp.ColumnSelection = getDatasetResp.Options.ColumnSelection
6567

6668
return nil
6769
}
@@ -110,9 +112,9 @@ func mapBucketTaggingToResponse(tagging *s3.GetBucketTaggingOutput, v *ReadObjec
110112
}
111113

112114
var filterObject struct {
113-
Type string `json:"_type"`
114-
Pattern string `json:"pattern"`
115-
ArrayFlattenDepth *int `json:"arrayFlattenDepth"`
115+
Type string `json:"_type"`
116+
Pattern string `json:"pattern"`
117+
ArrayFlattenDepth *int `json:"arrayFlattenDepth"`
116118
}
117119
if err := readJSONTagValue(tagging, "cs3.dataset-format", &filterObject); err != nil {
118120
return err

chaossearch/provider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func Provider() *schema.Provider {
4040
},
4141
},
4242
ResourcesMap: map[string]*schema.Resource{
43-
"chaossearch_object_group": resourceObjectGroup(),
43+
"chaossearch_object_group": resourceObjectGroup(),
4444
"chaossearch_indexing_state": resourceIndexingState(),
4545
},
4646
DataSourcesMap: map[string]*schema.Resource{

chaossearch/resource_object_group.go

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ func resourceObjectGroup() *schema.Resource {
7878
ForceNew: false,
7979
},
8080
"array_flatten_depth": {
81-
Type: schema.TypeInt,
82-
Default: 0,
83-
Description: "Array flattening level. 0 - disabled, -1 - unlimited, >1 - the respective flattening level",
84-
Optional: true,
85-
ForceNew: true,
81+
Type: schema.TypeInt,
82+
Default: 0,
83+
Description: "Array flattening level. 0 - disabled, -1 - unlimited, >1 - the respective flattening level",
84+
Optional: true,
85+
ForceNew: true,
8686
ValidateFunc: validation.IntAtLeast(-1),
8787
},
8888
"column_renames": {
@@ -95,6 +95,28 @@ func resourceObjectGroup() *schema.Resource {
9595
ForceNew: true,
9696
Description: "A map specifying names of columns to rename (keys) and what to rename them to (values)",
9797
},
98+
"column_selection": {
99+
Type: schema.TypeSet,
100+
Elem: &schema.Resource{
101+
Schema: map[string]*schema.Schema{
102+
"type": {
103+
Type: schema.TypeString,
104+
Required: true,
105+
},
106+
"includes": {
107+
Type: schema.TypeList,
108+
Elem: &schema.Schema{
109+
Type: schema.TypeString,
110+
},
111+
Required: true,
112+
},
113+
},
114+
115+
},
116+
Optional: true,
117+
ForceNew: true,
118+
Description: "List of fields in logs to include or exclude from parsing. If nothing is specified, all fields will be parsed",
119+
},
98120

99121
// Workaround. Otherwise Terraform fails with "All fields are ForceNew or Computed w/out Optional, Update is superfluous"
100122
"description": {
@@ -106,36 +128,45 @@ func resourceObjectGroup() *schema.Resource {
106128
}
107129
}
108130

109-
110-
111131
func resourceObjectGroupCreate(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
112132
c := meta.(*ProviderMeta).Client
113133

114134
// "unlimited" flattening represented as "null" in the api, and as -1 in the terraform module
115135
// because the terraform sdk doesn't support nil values in configs https://github.com/hashicorp/terraform-plugin-sdk/issues/261
116136
// We represent "null" as an int pointer to nil in the code.
117-
array_flatten_tf := data.Get("array_flatten_depth").(int)
118-
var array_flatten_cs *int
119-
if (array_flatten_tf == -1) {
137+
arrayFlattenTF := data.Get("array_flatten_depth").(int)
138+
var arrayFlattenCS *int
139+
if arrayFlattenTF == -1 {
120140
// -1 in terraform represents "null" in the ChaosSearch API call
121-
array_flatten_cs = nil
141+
arrayFlattenCS = nil
122142
} else {
123143
// any other value is passed as is
124-
array_flatten_cs = &array_flatten_tf
144+
arrayFlattenCS = &arrayFlattenTF
145+
}
146+
var columnSelection map[string]interface{}
147+
148+
if data.Get("column_selection").(*schema.Set).Len() > 0 {
149+
columnSelectionInterfaces := data.Get("column_selection").(*schema.Set).List()[0]
150+
columnSelectionInterface := columnSelectionInterfaces.(map[string]interface{})
151+
columnSelection = map[string]interface{}{
152+
"type": columnSelectionInterface["type"].(string),
153+
"includes": columnSelectionInterface["includes"].([]interface{}),
154+
}
125155
}
126156

127157
createObjectGroupRequest := &client.CreateObjectGroupRequest{
128-
Name: data.Get("name").(string),
129-
SourceBucket: data.Get("source_bucket").(string),
130-
FilterJSON: data.Get("filter_json").(string),
131-
Format: data.Get("format").(string),
132-
Compression: data.Get("compression").(string),
133-
LiveEventsSqsArn: data.Get("live_events_sqs_arn").(string),
134-
PartitionBy: data.Get("partition_by").(string),
135-
Pattern: data.Get("pattern").(string),
136-
IndexRetention: data.Get("index_retention").(int),
137-
ArrayFlattenDepth: array_flatten_cs,
138-
ColumnRenames: data.Get("column_renames").(map[string]interface{}),
158+
Name: data.Get("name").(string),
159+
SourceBucket: data.Get("source_bucket").(string),
160+
FilterJSON: data.Get("filter_json").(string),
161+
Format: data.Get("format").(string),
162+
Compression: data.Get("compression").(string),
163+
LiveEventsSqsArn: data.Get("live_events_sqs_arn").(string),
164+
PartitionBy: data.Get("partition_by").(string),
165+
Pattern: data.Get("pattern").(string),
166+
IndexRetention: data.Get("index_retention").(int),
167+
ArrayFlattenDepth: arrayFlattenCS,
168+
ColumnRenames: data.Get("column_renames").(map[string]interface{}),
169+
ColumnSelection: columnSelection,
139170
}
140171

141172
if err := c.CreateObjectGroup(ctx, createObjectGroupRequest); err != nil {
@@ -180,10 +211,12 @@ func resourceObjectGroupRead(ctx context.Context, data *schema.ResourceData, met
180211
data.Set("pattern", resp.Pattern)
181212
data.Set("source_bucket", resp.SourceBucket)
182213

214+
data.Set("column_selection", resp.ColumnSelection)
215+
183216
// "unlimited" flattening represented as "null" in the api, and as -1 in the terraform module
184217
// because the terraform sdk doesn't support nil values in configs https://github.com/hashicorp/terraform-plugin-sdk/issues/261
185218
// We represent "null" as an int pointer to nil in the code.
186-
if (resp.ArrayFlattenDepth == nil) {
219+
if resp.ArrayFlattenDepth == nil {
187220
data.Set("array_flatten_depth", -1)
188221
} else {
189222
data.Set("array_flatten_depth", resp.ArrayFlattenDepth)

examples/main.tf

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
terraform {
22
required_providers {
33
chaossearch = {
4-
versions = ["0.6.2"]
4+
version = "0.8.0"
55
source = "benzaita/chaossearch"
66
}
77
}
@@ -33,6 +33,14 @@ resource "chaossearch_object_group" "my-object-group" {
3333

3434
partition_by = "<regex>"
3535
array_flatten_depth = -1
36+
37+
column_selection {
38+
type = "whitelist"
39+
includes = [
40+
"host",
41+
"source",
42+
]
43+
}
3644
}
3745

3846
resource "chaossearch_indexing_state" "my-object-group" {

0 commit comments

Comments
 (0)