Skip to content

Commit da9cbea

Browse files
authored
Transformation resource (#68)
1 parent a0263ad commit da9cbea

File tree

6 files changed

+861
-0
lines changed

6 files changed

+861
-0
lines changed

docs/resources/transformation.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "segment_transformation Resource - terraform-provider-segment"
4+
subcategory: ""
5+
description: |-
6+
7+
---
8+
9+
# segment_transformation (Resource)
10+
11+
12+
13+
## Example Usage
14+
15+
```terraform
16+
# Configures a specific transformation
17+
resource "segment_transformation" "example" {
18+
source_id = segment_source.example.id
19+
name = "My transformation name"
20+
enabled = true
21+
if = "event = 'Bad Event'"
22+
new_event_name = "Good Event"
23+
property_renames = [
24+
{
25+
old_name = "old-name"
26+
new_name = "new-name"
27+
}
28+
]
29+
property_value_transformations = [
30+
{
31+
property_paths = ["properties.some-property", "context.some-property"],
32+
property_value = "some property value"
33+
},
34+
]
35+
fql_defined_properties = []
36+
}
37+
```
38+
39+
<!-- schema generated by tfplugindocs -->
40+
## Schema
41+
42+
### Required
43+
44+
- `enabled` (Boolean) If the Transformation is enabled.
45+
- `fql_defined_properties` (Attributes Set) Optional array for defining new properties in FQL. Currently limited to 1 property. (see [below for nested schema](#nestedatt--fql_defined_properties))
46+
- `if` (String) If statement (FQL) to match events.
47+
48+
For standard event matchers, use the following: Track -> "event='EVENT_NAME'" Identify -> "type='identify'" Group -> "type='group'"
49+
- `name` (String) The name of the Transformation.
50+
- `property_renames` (Attributes Set) Optional array for renaming properties collected by your events. (see [below for nested schema](#nestedatt--property_renames))
51+
- `property_value_transformations` (Attributes Set) Optional array for transforming properties and values collected by your events. Limited to 10 properties. (see [below for nested schema](#nestedatt--property_value_transformations))
52+
- `source_id` (String) The Source associated with the Transformation.
53+
54+
### Optional
55+
56+
- `destination_metadata_id` (String) The optional Destination metadata associated with the Transformation.
57+
- `new_event_name` (String) Optional new event name for renaming events. Works only for 'track' event type.
58+
59+
### Read-Only
60+
61+
- `id` (String) The id of the Transformation.
62+
63+
<a id="nestedatt--fql_defined_properties"></a>
64+
### Nested Schema for `fql_defined_properties`
65+
66+
Required:
67+
68+
- `fql` (String) The FQL expression used to compute the property.
69+
- `property_name` (String) The new property name.
70+
71+
72+
<a id="nestedatt--property_renames"></a>
73+
### Nested Schema for `property_renames`
74+
75+
Required:
76+
77+
- `new_name` (String) The new name to rename the property.
78+
- `old_name` (String) The old name of the property.
79+
80+
81+
<a id="nestedatt--property_value_transformations"></a>
82+
### Nested Schema for `property_value_transformations`
83+
84+
Required:
85+
86+
- `property_paths` (Set of String) The property paths. The maximum number of paths is 10.
87+
- `property_value` (String) The new value of the property paths.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Configures a specific transformation
2+
resource "segment_transformation" "example" {
3+
source_id = segment_source.example.id
4+
name = "My transformation name"
5+
enabled = true
6+
if = "event = 'Bad Event'"
7+
new_event_name = "Good Event"
8+
property_renames = [
9+
{
10+
old_name = "old-name"
11+
new_name = "new-name"
12+
}
13+
]
14+
property_value_transformations = [
15+
{
16+
property_paths = ["properties.some-property", "context.some-property"],
17+
property_value = "some property value"
18+
},
19+
]
20+
fql_defined_properties = []
21+
}
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
package models
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-framework/diag"
7+
"github.com/hashicorp/terraform-plugin-framework/types"
8+
"github.com/segmentio/public-api-sdk-go/api"
9+
)
10+
11+
type TransformationPlan struct {
12+
ID types.String `tfsdk:"id"`
13+
SourceID types.String `tfsdk:"source_id"`
14+
DestinationMetadataID types.String `tfsdk:"destination_metadata_id"`
15+
Name types.String `tfsdk:"name"`
16+
Enabled types.Bool `tfsdk:"enabled"`
17+
If types.String `tfsdk:"if"`
18+
NewEventName types.String `tfsdk:"new_event_name"`
19+
PropertyRenames types.Set `tfsdk:"property_renames"`
20+
PropertyValueTransformations types.Set `tfsdk:"property_value_transformations"`
21+
FQLDefinedProperties types.Set `tfsdk:"fql_defined_properties"`
22+
}
23+
24+
type TransformationState struct {
25+
ID types.String `tfsdk:"id"`
26+
SourceID types.String `tfsdk:"source_id"`
27+
DestinationMetadataID types.String `tfsdk:"destination_metadata_id"`
28+
Name types.String `tfsdk:"name"`
29+
Enabled types.Bool `tfsdk:"enabled"`
30+
If types.String `tfsdk:"if"`
31+
NewEventName types.String `tfsdk:"new_event_name"`
32+
PropertyRenames []PropertyRename `tfsdk:"property_renames"`
33+
PropertyValueTransformations []PropertyValueTransform `tfsdk:"property_value_transformations"`
34+
FQLDefinedProperties []FQLDefinedProperty `tfsdk:"fql_defined_properties"`
35+
}
36+
37+
type PropertyRename struct {
38+
OldName types.String `tfsdk:"old_name"`
39+
NewName types.String `tfsdk:"new_name"`
40+
}
41+
42+
type PropertyValueTransform struct {
43+
PropertyPaths []types.String `tfsdk:"property_paths"`
44+
PropertyValue types.String `tfsdk:"property_value"`
45+
}
46+
47+
type PropertyValueTransformPlan struct {
48+
PropertyPaths types.Set `tfsdk:"property_paths"`
49+
PropertyValue types.String `tfsdk:"property_value"`
50+
}
51+
52+
type FQLDefinedProperty struct {
53+
FQL types.String `tfsdk:"fql"`
54+
PropertyName types.String `tfsdk:"property_name"`
55+
}
56+
57+
func (t *TransformationState) Fill(transformation api.TransformationV1) {
58+
t.ID = types.StringValue(transformation.Id)
59+
t.SourceID = types.StringValue(transformation.SourceId)
60+
t.DestinationMetadataID = types.StringPointerValue(transformation.DestinationMetadataId)
61+
t.Name = types.StringValue(transformation.Name)
62+
t.Enabled = types.BoolValue(transformation.Enabled)
63+
t.If = types.StringValue(transformation.If)
64+
t.NewEventName = types.StringPointerValue(transformation.NewEventName)
65+
66+
// Fill PropertyRenames
67+
t.PropertyRenames = make([]PropertyRename, len(transformation.PropertyRenames))
68+
for i, pr := range transformation.PropertyRenames {
69+
t.PropertyRenames[i] = PropertyRename{
70+
OldName: types.StringValue(pr.OldName),
71+
NewName: types.StringValue(pr.NewName),
72+
}
73+
}
74+
75+
// Fill PropertyValueTransformations
76+
t.PropertyValueTransformations = make([]PropertyValueTransform, len(transformation.PropertyValueTransformations))
77+
for i, pvt := range transformation.PropertyValueTransformations {
78+
var paths []types.String
79+
for _, path := range pvt.PropertyPaths {
80+
paths = append(paths, types.StringValue(path))
81+
}
82+
83+
t.PropertyValueTransformations[i] = PropertyValueTransform{
84+
PropertyPaths: paths,
85+
PropertyValue: types.StringValue(pvt.PropertyValue),
86+
}
87+
}
88+
89+
// Fill FQLDefinedProperties
90+
t.FQLDefinedProperties = make([]FQLDefinedProperty, len(transformation.FqlDefinedProperties))
91+
for i, fdp := range transformation.FqlDefinedProperties {
92+
t.FQLDefinedProperties[i] = FQLDefinedProperty{
93+
FQL: types.StringValue(fdp.Fql),
94+
PropertyName: types.StringValue(fdp.PropertyName),
95+
}
96+
}
97+
}
98+
99+
func PropertyRenamesPlanToAPIValue(ctx context.Context, renames types.Set) ([]api.PropertyRenameV1, diag.Diagnostics) {
100+
apiRenames := []api.PropertyRenameV1{}
101+
102+
if !renames.IsNull() && !renames.IsUnknown() {
103+
stateRenames := []PropertyRename{}
104+
diags := renames.ElementsAs(ctx, &stateRenames, false)
105+
if diags.HasError() {
106+
return apiRenames, diags
107+
}
108+
for _, rename := range stateRenames {
109+
apiRenames = append(apiRenames, api.PropertyRenameV1{
110+
OldName: rename.OldName.ValueString(),
111+
NewName: rename.NewName.ValueString(),
112+
})
113+
}
114+
}
115+
116+
return apiRenames, diag.Diagnostics{}
117+
}
118+
119+
func PropertyValueTransformationsPlanToAPIValue(ctx context.Context, transforms types.Set) ([]api.PropertyValueTransformationV1, diag.Diagnostics) {
120+
apiTransforms := []api.PropertyValueTransformationV1{}
121+
122+
if !transforms.IsNull() && !transforms.IsUnknown() {
123+
stateTransforms := []PropertyValueTransformPlan{}
124+
diags := transforms.ElementsAs(ctx, &stateTransforms, false)
125+
if diags.HasError() {
126+
return apiTransforms, diags
127+
}
128+
for _, transform := range stateTransforms {
129+
paths := []string{}
130+
diags := transform.PropertyPaths.ElementsAs(ctx, &paths, false)
131+
if diags.HasError() {
132+
return apiTransforms, diags
133+
}
134+
135+
apiTransforms = append(apiTransforms, api.PropertyValueTransformationV1{
136+
PropertyPaths: paths,
137+
PropertyValue: transform.PropertyValue.ValueString(),
138+
})
139+
}
140+
}
141+
142+
return apiTransforms, diag.Diagnostics{}
143+
}
144+
145+
func FQLDefinedPropertiesPlanToAPIValue(ctx context.Context, properties types.Set) ([]api.FQLDefinedPropertyV1, diag.Diagnostics) {
146+
apiPreperties := []api.FQLDefinedPropertyV1{}
147+
148+
if !properties.IsNull() && !properties.IsUnknown() {
149+
stateProperties := []FQLDefinedProperty{}
150+
diags := properties.ElementsAs(ctx, &stateProperties, false)
151+
if diags.HasError() {
152+
return apiPreperties, diags
153+
}
154+
for _, property := range stateProperties {
155+
apiPreperties = append(apiPreperties, api.FQLDefinedPropertyV1{
156+
Fql: property.FQL.ValueString(),
157+
PropertyName: property.PropertyName.ValueString(),
158+
})
159+
}
160+
}
161+
162+
return apiPreperties, diag.Diagnostics{}
163+
}

internal/provider/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ func (p *segmentProvider) Resources(_ context.Context) []func() resource.Resourc
156156
NewDestinationSubscriptionResource,
157157
NewSourceTrackingPlanConnectionResource,
158158
NewReverseETLModelResource,
159+
NewTransformationResource,
159160
NewInsertFunctionInstanceResource,
160161
}
161162
}

0 commit comments

Comments
 (0)