-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new packages for datasourcetimeouts and resourcetimeouts that mak…
…e use of type-specific schema and attributes
- Loading branch information
1 parent
8f55964
commit aa0bcd6
Showing
8 changed files
with
756 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package datasourcetimeouts | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework-timeouts/internal/validators" | ||
) | ||
|
||
const ( | ||
attributeNameCreate = "create" | ||
attributeNameRead = "read" | ||
attributeNameUpdate = "update" | ||
attributeNameDelete = "delete" | ||
) | ||
|
||
// Opts is used as an argument to Block and Attributes to indicate which attributes | ||
// should be created. | ||
type Opts struct { | ||
Create bool | ||
Read bool | ||
Update bool | ||
Delete bool | ||
} | ||
|
||
// Block returns a schema.Block containing attributes for each of the fields | ||
// in Opts which are set to true. Each attribute is defined as types.StringType | ||
// and optional. A validator is used to verify that the value assigned to an | ||
// attribute can be parsed as time.Duration. | ||
func Block(ctx context.Context, opts Opts) schema.Block { | ||
return schema.SingleNestedBlock{ | ||
Attributes: attributesMap(opts), | ||
} | ||
} | ||
|
||
// BlockAll returns a schema.Block containing attributes for each of create, read, | ||
// update and delete. Each attribute is defined as types.StringType and optional. | ||
// A validator is used to verify that the value assigned to an attribute can be | ||
// parsed as time.Duration. | ||
func BlockAll(ctx context.Context) schema.Block { | ||
return Block(ctx, Opts{ | ||
Create: true, | ||
Read: true, | ||
Update: true, | ||
Delete: true, | ||
}) | ||
} | ||
|
||
// Attributes returns a schema.SingleNestedAttribute | ||
// which contains attributes for each of the fields in Opts which are set to true. | ||
// Each attribute is defined as types.StringType and optional. A validator is used | ||
// to verify that the value assigned to an attribute can be parsed as time.Duration. | ||
func Attributes(ctx context.Context, opts Opts) schema.Attribute { | ||
return schema.SingleNestedAttribute{ | ||
Optional: true, | ||
Attributes: attributesMap(opts), | ||
} | ||
} | ||
|
||
// AttributesAll returns a schema.SingleNestedAttribute | ||
// which contains attributes for each of create, read, update and delete. Each | ||
// attribute is defined as types.StringType and optional. A validator is used to | ||
// verify that the value assigned to an attribute can be parsed as time.Duration. | ||
func AttributesAll(ctx context.Context) schema.Attribute { | ||
return Attributes(ctx, Opts{ | ||
Create: true, | ||
Read: true, | ||
Update: true, | ||
Delete: true, | ||
}) | ||
} | ||
|
||
func attributesMap(opts Opts) map[string]schema.Attribute { | ||
attributes := map[string]schema.Attribute{} | ||
attribute := schema.StringAttribute{ | ||
Optional: true, | ||
Validators: []validator.String{ | ||
validators.TimeDurationString(), | ||
}, | ||
} | ||
|
||
if opts.Create { | ||
attributes[attributeNameCreate] = attribute | ||
} | ||
|
||
if opts.Read { | ||
attributes[attributeNameRead] = attribute | ||
} | ||
|
||
if opts.Update { | ||
attributes[attributeNameUpdate] = attribute | ||
} | ||
|
||
if opts.Delete { | ||
attributes[attributeNameDelete] = attribute | ||
} | ||
|
||
return attributes | ||
} |
Oops, something went wrong.