|
| 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 | +} |
0 commit comments