Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transformation resource #68

Merged
merged 5 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions docs/resources/transformation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "segment_transformation Resource - terraform-provider-segment"
subcategory: ""
description: |-

---

# segment_transformation (Resource)



## Example Usage

```terraform
# Configures a specific transformation
resource "segment_transformation" "example" {
source_id = segment_source.example.id
name = "My transformation name"
enabled = true
if = "event = 'Bad Event'"
new_event_name = "Good Event"
property_renames = [
{
old_name = "old-name"
new_name = "new-name"
}
]
property_value_transformations = [
{
property_paths = ["properties.some-property", "context.some-property"],
property_value = "some property value"
},
]
fql_defined_properties = []
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `enabled` (Boolean) If the Transformation is enabled.
- `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))
- `if` (String) If statement (FQL) to match events.

For standard event matchers, use the following: Track -> "event='EVENT_NAME'" Identify -> "type='identify'" Group -> "type='group'"
- `name` (String) The name of the Transformation.
- `property_renames` (Attributes Set) Optional array for renaming properties collected by your events. (see [below for nested schema](#nestedatt--property_renames))
- `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))
- `source_id` (String) The Source associated with the Transformation.

### Optional

- `destination_metadata_id` (String) The optional Destination metadata associated with the Transformation.
- `new_event_name` (String) Optional new event name for renaming events. Works only for 'track' event type.

### Read-Only

- `id` (String) The id of the Transformation.

<a id="nestedatt--fql_defined_properties"></a>
### Nested Schema for `fql_defined_properties`

Required:

- `fql` (String) The FQL expression used to compute the property.
- `property_name` (String) The new property name.


<a id="nestedatt--property_renames"></a>
### Nested Schema for `property_renames`

Required:

- `new_name` (String) The new name to rename the property.
- `old_name` (String) The old name of the property.


<a id="nestedatt--property_value_transformations"></a>
### Nested Schema for `property_value_transformations`

Required:

- `property_paths` (Set of String) The property paths. The maximum number of paths is 10.
- `property_value` (String) The new value of the property paths.
21 changes: 21 additions & 0 deletions examples/resources/segment_transformation/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Configures a specific transformation
resource "segment_transformation" "example" {
source_id = segment_source.example.id
name = "My transformation name"
enabled = true
if = "event = 'Bad Event'"
new_event_name = "Good Event"
property_renames = [
{
old_name = "old-name"
new_name = "new-name"
}
]
property_value_transformations = [
{
property_paths = ["properties.some-property", "context.some-property"],
property_value = "some property value"
},
]
fql_defined_properties = []
}
163 changes: 163 additions & 0 deletions internal/provider/models/transformation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package models

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/segmentio/public-api-sdk-go/api"
)

type TransformationPlan struct {
ID types.String `tfsdk:"id"`
SourceID types.String `tfsdk:"source_id"`
DestinationMetadataID types.String `tfsdk:"destination_metadata_id"`
Name types.String `tfsdk:"name"`
Enabled types.Bool `tfsdk:"enabled"`
If types.String `tfsdk:"if"`
NewEventName types.String `tfsdk:"new_event_name"`
PropertyRenames types.Set `tfsdk:"property_renames"`
PropertyValueTransformations types.Set `tfsdk:"property_value_transformations"`
FQLDefinedProperties types.Set `tfsdk:"fql_defined_properties"`
}

type TransformationState struct {
ID types.String `tfsdk:"id"`
SourceID types.String `tfsdk:"source_id"`
DestinationMetadataID types.String `tfsdk:"destination_metadata_id"`
Name types.String `tfsdk:"name"`
Enabled types.Bool `tfsdk:"enabled"`
If types.String `tfsdk:"if"`
NewEventName types.String `tfsdk:"new_event_name"`
PropertyRenames []PropertyRename `tfsdk:"property_renames"`
PropertyValueTransformations []PropertyValueTransform `tfsdk:"property_value_transformations"`
FQLDefinedProperties []FQLDefinedProperty `tfsdk:"fql_defined_properties"`
}

type PropertyRename struct {
OldName types.String `tfsdk:"old_name"`
NewName types.String `tfsdk:"new_name"`
}

type PropertyValueTransform struct {
PropertyPaths []types.String `tfsdk:"property_paths"`
PropertyValue types.String `tfsdk:"property_value"`
}

type PropertyValueTransformPlan struct {
PropertyPaths types.Set `tfsdk:"property_paths"`
PropertyValue types.String `tfsdk:"property_value"`
}

type FQLDefinedProperty struct {
FQL types.String `tfsdk:"fql"`
PropertyName types.String `tfsdk:"property_name"`
}

func (t *TransformationState) Fill(transformation api.TransformationV1) {
t.ID = types.StringValue(transformation.Id)
t.SourceID = types.StringValue(transformation.SourceId)
t.DestinationMetadataID = types.StringPointerValue(transformation.DestinationMetadataId)
t.Name = types.StringValue(transformation.Name)
t.Enabled = types.BoolValue(transformation.Enabled)
t.If = types.StringValue(transformation.If)
t.NewEventName = types.StringPointerValue(transformation.NewEventName)

// Fill PropertyRenames
t.PropertyRenames = make([]PropertyRename, len(transformation.PropertyRenames))
for i, pr := range transformation.PropertyRenames {
t.PropertyRenames[i] = PropertyRename{
OldName: types.StringValue(pr.OldName),
NewName: types.StringValue(pr.NewName),
}
}

// Fill PropertyValueTransformations
t.PropertyValueTransformations = make([]PropertyValueTransform, len(transformation.PropertyValueTransformations))
for i, pvt := range transformation.PropertyValueTransformations {
var paths []types.String
for _, path := range pvt.PropertyPaths {
paths = append(paths, types.StringValue(path))
}

t.PropertyValueTransformations[i] = PropertyValueTransform{
PropertyPaths: paths,
PropertyValue: types.StringValue(pvt.PropertyValue),
}
}

// Fill FQLDefinedProperties
t.FQLDefinedProperties = make([]FQLDefinedProperty, len(transformation.FqlDefinedProperties))
for i, fdp := range transformation.FqlDefinedProperties {
t.FQLDefinedProperties[i] = FQLDefinedProperty{
FQL: types.StringValue(fdp.Fql),
PropertyName: types.StringValue(fdp.PropertyName),
}
}
}

func PropertyRenamesPlanToAPIValue(ctx context.Context, renames types.Set) ([]api.PropertyRenameV1, diag.Diagnostics) {
apiRenames := []api.PropertyRenameV1{}

if !renames.IsNull() && !renames.IsUnknown() {
stateRenames := []PropertyRename{}
diags := renames.ElementsAs(ctx, &stateRenames, false)
if diags.HasError() {
return apiRenames, diags
}
for _, rename := range stateRenames {
apiRenames = append(apiRenames, api.PropertyRenameV1{
OldName: rename.OldName.ValueString(),
NewName: rename.NewName.ValueString(),
})
}
}

return apiRenames, diag.Diagnostics{}
}

func PropertyValueTransformationsPlanToAPIValue(ctx context.Context, transforms types.Set) ([]api.PropertyValueTransformationV1, diag.Diagnostics) {
apiTransforms := []api.PropertyValueTransformationV1{}

if !transforms.IsNull() && !transforms.IsUnknown() {
stateTransforms := []PropertyValueTransformPlan{}
diags := transforms.ElementsAs(ctx, &stateTransforms, false)
if diags.HasError() {
return apiTransforms, diags
}
for _, transform := range stateTransforms {
paths := []string{}
diags := transform.PropertyPaths.ElementsAs(ctx, &paths, false)
if diags.HasError() {
return apiTransforms, diags
}

apiTransforms = append(apiTransforms, api.PropertyValueTransformationV1{
PropertyPaths: paths,
PropertyValue: transform.PropertyValue.ValueString(),
})
}
}

return apiTransforms, diag.Diagnostics{}
}

func FQLDefinedPropertiesPlanToAPIValue(ctx context.Context, properties types.Set) ([]api.FQLDefinedPropertyV1, diag.Diagnostics) {
apiPreperties := []api.FQLDefinedPropertyV1{}

if !properties.IsNull() && !properties.IsUnknown() {
stateProperties := []FQLDefinedProperty{}
diags := properties.ElementsAs(ctx, &stateProperties, false)
if diags.HasError() {
return apiPreperties, diags
}
for _, property := range stateProperties {
apiPreperties = append(apiPreperties, api.FQLDefinedPropertyV1{
Fql: property.FQL.ValueString(),
PropertyName: property.PropertyName.ValueString(),
})
}
}

return apiPreperties, diag.Diagnostics{}
}
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ func (p *segmentProvider) Resources(_ context.Context) []func() resource.Resourc
NewDestinationSubscriptionResource,
NewSourceTrackingPlanConnectionResource,
NewReverseETLModelResource,
NewTransformationResource,
NewInsertFunctionInstanceResource,
}
}
Expand Down
Loading