|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "github.com/hashicorp/go-tfe" |
| 6 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 7 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/types/basetypes" |
| 10 | + "math/big" |
| 11 | +) |
| 12 | + |
| 13 | +type modelTFEDataRetentionPolicy struct { |
| 14 | + ID types.String `tfsdk:"id"` |
| 15 | + Organization types.String `tfsdk:"organization"` |
| 16 | + WorkspaceID types.String `tfsdk:"workspace_id"` |
| 17 | + DeleteOlderThan types.Object `tfsdk:"delete_older_than"` |
| 18 | + DontDelete types.Object `tfsdk:"dont_delete"` |
| 19 | +} |
| 20 | + |
| 21 | +type modelTFEDeleteOlderThan struct { |
| 22 | + Days types.Number `tfsdk:"days"` |
| 23 | +} |
| 24 | + |
| 25 | +func (m modelTFEDeleteOlderThan) AttributeTypes() map[string]attr.Type { |
| 26 | + return map[string]attr.Type{ |
| 27 | + "days": types.NumberType, |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func DontDeleteEmptyObject() basetypes.ObjectValue { |
| 32 | + object, diags := types.ObjectValue(map[string]attr.Type{}, map[string]attr.Value{}) |
| 33 | + if diags.HasError() { |
| 34 | + panic(diags.Errors()) |
| 35 | + } |
| 36 | + return object |
| 37 | +} |
| 38 | + |
| 39 | +func modelFromTFEDataRetentionPolicyDeleteOlder(ctx context.Context, model modelTFEDataRetentionPolicy, deleteOlder *tfe.DataRetentionPolicyDeleteOlder) (modelTFEDataRetentionPolicy, diag.Diagnostics) { |
| 40 | + deleteOlderThan := modelTFEDeleteOlderThan{ |
| 41 | + Days: types.NumberValue(big.NewFloat(float64(deleteOlder.DeleteOlderThanNDays))), |
| 42 | + } |
| 43 | + deleteOlderThanObject, diags := types.ObjectValueFrom(ctx, deleteOlderThan.AttributeTypes(), deleteOlderThan) |
| 44 | + |
| 45 | + organization := types.StringNull() |
| 46 | + if model.WorkspaceID.IsNull() { |
| 47 | + organization = model.Organization |
| 48 | + } |
| 49 | + |
| 50 | + return modelTFEDataRetentionPolicy{ |
| 51 | + ID: types.StringValue(deleteOlder.ID), |
| 52 | + Organization: organization, |
| 53 | + WorkspaceID: model.WorkspaceID, |
| 54 | + DeleteOlderThan: deleteOlderThanObject, |
| 55 | + DontDelete: types.ObjectNull(map[string]attr.Type{}), |
| 56 | + }, diags |
| 57 | +} |
| 58 | + |
| 59 | +func modelFromTFEDataRetentionPolicyDontDelete(model modelTFEDataRetentionPolicy, dontDelete *tfe.DataRetentionPolicyDontDelete) modelTFEDataRetentionPolicy { |
| 60 | + organization := types.StringNull() |
| 61 | + if model.WorkspaceID.IsNull() { |
| 62 | + organization = model.Organization |
| 63 | + } |
| 64 | + |
| 65 | + return modelTFEDataRetentionPolicy{ |
| 66 | + ID: types.StringValue(dontDelete.ID), |
| 67 | + Organization: organization, |
| 68 | + WorkspaceID: model.WorkspaceID, |
| 69 | + DeleteOlderThan: types.ObjectNull(modelTFEDeleteOlderThan{}.AttributeTypes()), |
| 70 | + DontDelete: DontDeleteEmptyObject(), |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func modelFromTFEDataRetentionPolicyChoice(ctx context.Context, model modelTFEDataRetentionPolicy, choice *tfe.DataRetentionPolicyChoice) (modelTFEDataRetentionPolicy, diag.Diagnostics) { |
| 75 | + if choice.DataRetentionPolicyDeleteOlder != nil { |
| 76 | + return modelFromTFEDataRetentionPolicyDeleteOlder(ctx, model, choice.DataRetentionPolicyDeleteOlder) |
| 77 | + } |
| 78 | + |
| 79 | + var emptyDiag []diag.Diagnostic |
| 80 | + return modelFromTFEDataRetentionPolicyDontDelete(model, choice.DataRetentionPolicyDontDelete), emptyDiag |
| 81 | +} |
0 commit comments