|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package timeouts |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/list/schema" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 13 | + |
| 14 | + "github.com/hashicorp/terraform-plugin-framework-timeouts/internal/validators" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + attributeNameList = "list" |
| 19 | +) |
| 20 | + |
| 21 | +// Opts is used as an argument to BlockWithOpts and AttributesWithOpts to indicate |
| 22 | +// whether supplied descriptions should override default descriptions. |
| 23 | +type Opts struct { |
| 24 | + ListDescription string |
| 25 | +} |
| 26 | + |
| 27 | +// BlockWithOpts returns a schema.Block containing attributes for `Open`, which is |
| 28 | +// defined as types.StringType and optional. A validator is used to verify |
| 29 | +// that the value assigned to `Open` can be parsed as time.Duration. The supplied |
| 30 | +// Opts are used to override defaults. |
| 31 | +func BlockWithOpts(ctx context.Context, opts Opts) schema.Block { |
| 32 | + return schema.SingleNestedBlock{ |
| 33 | + Attributes: attributesMap(opts), |
| 34 | + CustomType: Type{ |
| 35 | + ObjectType: types.ObjectType{ |
| 36 | + AttrTypes: attrTypesMap(), |
| 37 | + }, |
| 38 | + }, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +// Block returns a schema.Block containing attributes for `Open`, which is |
| 43 | +// defined as types.StringType and optional. A validator is used to verify |
| 44 | +// that the value assigned to `Open` can be parsed as time.Duration. |
| 45 | +func Block(ctx context.Context) schema.Block { |
| 46 | + return schema.SingleNestedBlock{ |
| 47 | + Attributes: attributesMap(Opts{}), |
| 48 | + CustomType: Type{ |
| 49 | + ObjectType: types.ObjectType{ |
| 50 | + AttrTypes: attrTypesMap(), |
| 51 | + }, |
| 52 | + }, |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// AttributesWithOpts returns a schema.SingleNestedAttribute which contains an |
| 57 | +// attribute for `Open`, which is defined as types.StringType and optional. |
| 58 | +// A validator is used to verify that the value assigned to an attribute |
| 59 | +// can be parsed as time.Duration. The supplied Opts are used to override defaults. |
| 60 | +func AttributesWithOpts(ctx context.Context, opts Opts) schema.Attribute { |
| 61 | + return schema.SingleNestedAttribute{ |
| 62 | + Attributes: attributesMap(opts), |
| 63 | + CustomType: Type{ |
| 64 | + ObjectType: types.ObjectType{ |
| 65 | + AttrTypes: attrTypesMap(), |
| 66 | + }, |
| 67 | + }, |
| 68 | + Optional: true, |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +// Attributes returns a schema.SingleNestedAttribute which contains an |
| 73 | +// attribute for `Open`, which is defined as types.StringType and optional. |
| 74 | +// A validator is used to verify that the value assigned to an attribute |
| 75 | +// can be parsed as time.Duration. |
| 76 | +func Attributes(ctx context.Context) schema.Attribute { |
| 77 | + return schema.SingleNestedAttribute{ |
| 78 | + Attributes: attributesMap(Opts{}), |
| 79 | + CustomType: Type{ |
| 80 | + ObjectType: types.ObjectType{ |
| 81 | + AttrTypes: attrTypesMap(), |
| 82 | + }, |
| 83 | + }, |
| 84 | + Optional: true, |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func attributesMap(opts Opts) map[string]schema.Attribute { |
| 89 | + attribute := schema.StringAttribute{ |
| 90 | + Optional: true, |
| 91 | + Description: `A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) ` + |
| 92 | + `consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are ` + |
| 93 | + `"s" (seconds), "m" (minutes), "h" (hours).`, |
| 94 | + Validators: []validator.String{ |
| 95 | + validators.TimeDuration(), |
| 96 | + }, |
| 97 | + } |
| 98 | + |
| 99 | + if opts.ListDescription != "" { |
| 100 | + attribute.Description = opts.ListDescription |
| 101 | + } |
| 102 | + |
| 103 | + return map[string]schema.Attribute{ |
| 104 | + attributeNameList: attribute, |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func attrTypesMap() map[string]attr.Type { |
| 109 | + return map[string]attr.Type{ |
| 110 | + attributeNameList: types.StringType, |
| 111 | + } |
| 112 | +} |
0 commit comments