Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/unreleased/FEATURES-20251022-083255.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: FEATURES
body: 'action/timeouts: Adds functions and types for action timeouts'
time: 2025-10-22T08:32:55.169797074-05:00
custom:
Issue: "205"
112 changes: 112 additions & 0 deletions action/timeouts/schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package timeouts

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/action/schema"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"

"github.com/hashicorp/terraform-plugin-framework-timeouts/internal/validators"
)

const (
attributeNameInvoke = "invoke"
)

// Opts is used as an argument to BlockWithOpts and AttributesWithOpts to indicate
// whether supplied descriptions should override default descriptions.
type Opts struct {
InvokeDescription string
}

// BlockWithOpts returns a schema.Block containing attributes for `Invoke`, which is
// defined as types.StringType and optional. A validator is used to verify
// that the value assigned to `Invoke` can be parsed as time.Duration. The supplied
// Opts are used to override defaults.
func BlockWithOpts(ctx context.Context, opts Opts) schema.Block {
return schema.SingleNestedBlock{
Attributes: attributesMap(opts),
CustomType: Type{
ObjectType: types.ObjectType{
AttrTypes: attrTypesMap(),
},
},
}
}

// Block returns a schema.Block containing attributes for `Invoke`, which is
// defined as types.StringType and optional. A validator is used to verify
// that the value assigned to `Invoke` can be parsed as time.Duration.
func Block(ctx context.Context) schema.Block {
return schema.SingleNestedBlock{
Attributes: attributesMap(Opts{}),
CustomType: Type{
ObjectType: types.ObjectType{
AttrTypes: attrTypesMap(),
},
},
}
}

// AttributesWithOpts returns a schema.SingleNestedAttribute which contains an
// attribute for `Invoke`, which 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. The supplied Opts are used to override defaults.
func AttributesWithOpts(ctx context.Context, opts Opts) schema.Attribute {
return schema.SingleNestedAttribute{
Attributes: attributesMap(opts),
CustomType: Type{
ObjectType: types.ObjectType{
AttrTypes: attrTypesMap(),
},
},
Optional: true,
}
}

// Attributes returns a schema.SingleNestedAttribute which contains an
// attribute for `Invoke`, which 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) schema.Attribute {
return schema.SingleNestedAttribute{
Attributes: attributesMap(Opts{}),
CustomType: Type{
ObjectType: types.ObjectType{
AttrTypes: attrTypesMap(),
},
},
Optional: true,
}
}

func attributesMap(opts Opts) map[string]schema.Attribute {
attribute := schema.StringAttribute{
Optional: true,
Description: `A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) ` +
`consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are ` +
`"s" (seconds), "m" (minutes), "h" (hours).`,
Validators: []validator.String{
validators.TimeDuration(),
},
}

if opts.InvokeDescription != "" {
attribute.Description = opts.InvokeDescription
}

return map[string]schema.Attribute{
attributeNameInvoke: attribute,
}
}

func attrTypesMap() map[string]attr.Type {
return map[string]attr.Type{
attributeNameInvoke: types.StringType,
}
}
247 changes: 247 additions & 0 deletions action/timeouts/schema_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package timeouts_test

import (
"context"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/action/schema"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"

"github.com/hashicorp/terraform-plugin-framework-timeouts/action/timeouts"
"github.com/hashicorp/terraform-plugin-framework-timeouts/internal/validators"
)

func TestBlockWithOpts(t *testing.T) {
t.Parallel()

type testCase struct {
opts timeouts.Opts
expected schema.Block
}
tests := map[string]testCase{
"empty-opts": {
opts: timeouts.Opts{},
expected: schema.SingleNestedBlock{
CustomType: timeouts.Type{
ObjectType: types.ObjectType{
AttrTypes: map[string]attr.Type{
"invoke": types.StringType,
},
},
},
Attributes: map[string]schema.Attribute{
"invoke": schema.StringAttribute{
Optional: true,
Description: `A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) ` +
`consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are ` +
`"s" (seconds), "m" (minutes), "h" (hours).`,
Validators: []validator.String{
validators.TimeDuration(),
},
},
},
},
},
"invoke-opts-description": {
opts: timeouts.Opts{
InvokeDescription: "invoke description",
},
expected: schema.SingleNestedBlock{
CustomType: timeouts.Type{
ObjectType: types.ObjectType{
AttrTypes: map[string]attr.Type{
"invoke": types.StringType,
},
},
},
Attributes: map[string]schema.Attribute{
"invoke": schema.StringAttribute{
Optional: true,
Description: "invoke description",
Validators: []validator.String{
validators.TimeDuration(),
},
},
},
},
},
}

for name, test := range tests {

t.Run(name, func(t *testing.T) {
t.Parallel()
actual := timeouts.BlockWithOpts(context.Background(), test.opts)

if diff := cmp.Diff(actual, test.expected); diff != "" {
t.Errorf("unexpected block difference: %s", diff)
}
})
}
}

func TestBlock(t *testing.T) {
t.Parallel()

type testCase struct {
expected schema.Block
}
tests := map[string]testCase{
"invoke": {
expected: schema.SingleNestedBlock{
CustomType: timeouts.Type{
ObjectType: types.ObjectType{
AttrTypes: map[string]attr.Type{
"invoke": types.StringType,
},
},
},
Attributes: map[string]schema.Attribute{
"invoke": schema.StringAttribute{
Optional: true,
Description: `A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) ` +
`consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are ` +
`"s" (seconds), "m" (minutes), "h" (hours).`,
Validators: []validator.String{
validators.TimeDuration(),
},
},
},
},
},
}

for name, test := range tests {

t.Run(name, func(t *testing.T) {
t.Parallel()
actual := timeouts.Block(context.Background())

if diff := cmp.Diff(actual, test.expected); diff != "" {
t.Errorf("unexpected block difference: %s", diff)
}
})
}
}

func TestAttributesWithOpts(t *testing.T) {
t.Parallel()

type testCase struct {
opts timeouts.Opts
expected schema.Attribute
}
tests := map[string]testCase{
"empty-opts": {
opts: timeouts.Opts{},
expected: schema.SingleNestedAttribute{
Attributes: map[string]schema.Attribute{
"invoke": schema.StringAttribute{
Optional: true,
Description: `A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) ` +
`consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are ` +
`"s" (seconds), "m" (minutes), "h" (hours).`,
Validators: []validator.String{
validators.TimeDuration(),
},
},
},
CustomType: timeouts.Type{
ObjectType: types.ObjectType{
AttrTypes: map[string]attr.Type{
"invoke": types.StringType,
},
},
},
Optional: true,
},
},
"invoke-opts-description": {
opts: timeouts.Opts{
InvokeDescription: "invoke description",
},
expected: schema.SingleNestedAttribute{
Attributes: map[string]schema.Attribute{
"invoke": schema.StringAttribute{
Optional: true,
Description: "invoke description",
Validators: []validator.String{
validators.TimeDuration(),
},
},
},
CustomType: timeouts.Type{
ObjectType: types.ObjectType{
AttrTypes: map[string]attr.Type{
"invoke": types.StringType,
},
},
},
Optional: true,
},
},
}

for name, test := range tests {

t.Run(name, func(t *testing.T) {
t.Parallel()
actual := timeouts.AttributesWithOpts(context.Background(), test.opts)

if diff := cmp.Diff(actual, test.expected); diff != "" {
t.Errorf("unexpected block difference: %s", diff)
}
})
}
}

func TestAttributes(t *testing.T) {
t.Parallel()

type testCase struct {
expected schema.Attribute
}
tests := map[string]testCase{
"invoke": {
expected: schema.SingleNestedAttribute{
Attributes: map[string]schema.Attribute{
"invoke": schema.StringAttribute{
Optional: true,
Description: `A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) ` +
`consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are ` +
`"s" (seconds), "m" (minutes), "h" (hours).`,
Validators: []validator.String{
validators.TimeDuration(),
},
},
},
CustomType: timeouts.Type{
ObjectType: types.ObjectType{
AttrTypes: map[string]attr.Type{
"invoke": types.StringType,
},
},
},
Optional: true,
},
},
}

for name, test := range tests {

t.Run(name, func(t *testing.T) {
t.Parallel()
actual := timeouts.Attributes(context.Background())

if diff := cmp.Diff(actual, test.expected); diff != "" {
t.Errorf("unexpected block difference: %s", diff)
}
})
}
}
Loading