|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/attr/xattr" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/types/basetypes" |
| 13 | + "github.com/hashicorp/terraform-plugin-go/tftypes" |
| 14 | +) |
| 15 | + |
| 16 | +var ( |
| 17 | + _ basetypes.StringTypable = CustomTimestampType{} |
| 18 | + _ xattr.TypeWithValidate = CustomTimestampType{} |
| 19 | +) |
| 20 | + |
| 21 | +type CustomTimestampType struct { |
| 22 | + basetypes.StringType |
| 23 | +} |
| 24 | + |
| 25 | +func (t CustomTimestampType) Equal(o attr.Type) bool { |
| 26 | + other, ok := o.(CustomTimestampType) |
| 27 | + |
| 28 | + if !ok { |
| 29 | + return false |
| 30 | + } |
| 31 | + return t.StringType.Equal(other.StringType) |
| 32 | +} |
| 33 | + |
| 34 | +func (t CustomTimestampType) String() string { |
| 35 | + return "CustomTimestampType" |
| 36 | +} |
| 37 | + |
| 38 | +func (t CustomTimestampType) ValueFromString(ctx context.Context, in basetypes.StringValue) (basetypes.StringValuable, diag.Diagnostics) { |
| 39 | + value := CustomTimestampValue{ |
| 40 | + StringValue: in, |
| 41 | + } |
| 42 | + |
| 43 | + return value, nil |
| 44 | +} |
| 45 | + |
| 46 | +func (t CustomTimestampType) ValueFromTerraform(ctx context.Context, in tftypes.Value) (attr.Value, error) { |
| 47 | + attrValue, err := t.StringType.ValueFromTerraform(ctx, in) |
| 48 | + |
| 49 | + if err != nil { |
| 50 | + return nil, err |
| 51 | + } |
| 52 | + |
| 53 | + stringValue, ok := attrValue.(basetypes.StringValue) |
| 54 | + |
| 55 | + if !ok { |
| 56 | + return nil, fmt.Errorf("unexpected value type of %T", attrValue) |
| 57 | + } |
| 58 | + |
| 59 | + stringValuable, diags := t.ValueFromString(ctx, stringValue) |
| 60 | + |
| 61 | + if diags.HasError() { |
| 62 | + return nil, fmt.Errorf("unexpected error converting StringValue to StringValuable: %v", diags) |
| 63 | + } |
| 64 | + |
| 65 | + return stringValuable, nil |
| 66 | +} |
| 67 | + |
| 68 | +func (t CustomTimestampType) ValueType(ctx context.Context) attr.Value { |
| 69 | + return CustomTimestampValue{} |
| 70 | +} |
| 71 | + |
| 72 | +func (t CustomTimestampType) Validate(ctx context.Context, value tftypes.Value, valuePath path.Path) diag.Diagnostics { |
| 73 | + if value.IsNull() || !value.IsKnown() { |
| 74 | + return nil |
| 75 | + } |
| 76 | + |
| 77 | + var diags diag.Diagnostics |
| 78 | + var valueString string |
| 79 | + |
| 80 | + if err := value.As(&valueString); err != nil { |
| 81 | + diags.AddAttributeError( |
| 82 | + valuePath, |
| 83 | + "Invalid Terraform Value", |
| 84 | + "An unexpected error occurred while attempting to convert a Terraform value to a string. "+ |
| 85 | + "This generally is an issue with the provider schema implementation. "+ |
| 86 | + "Please contact the provider developers.\n\n"+ |
| 87 | + "Path: "+valuePath.String()+"\n"+ |
| 88 | + "Error: "+err.Error(), |
| 89 | + ) |
| 90 | + return diags |
| 91 | + } |
| 92 | + |
| 93 | + if valueString == "infinity" { |
| 94 | + return diags |
| 95 | + } |
| 96 | + |
| 97 | + if _, err := time.Parse(time.DateTime, valueString); err != nil { |
| 98 | + diags.AddAttributeError( |
| 99 | + valuePath, |
| 100 | + "Invalid DateTime String Value", |
| 101 | + "An unexpected error occurred while converting a string value that was expected to be a DateTime format. "+ |
| 102 | + "The string format is 'YYYY-MM-DD HH:MM:SS', such as '2006-01-02 15:04:05' or '2006-01-02 15:04:05'.\n\n"+ |
| 103 | + "Path: "+valuePath.String()+"\n"+ |
| 104 | + "Given Value: "+valueString+"\n"+ |
| 105 | + "Error: "+err.Error(), |
| 106 | + ) |
| 107 | + |
| 108 | + return diags |
| 109 | + } |
| 110 | + |
| 111 | + return diags |
| 112 | +} |
0 commit comments