Skip to content

Commit 33d07d2

Browse files
committed
feat(docs) extend contribution example with timeouts
1 parent 445bca4 commit 33d07d2

1 file changed

Lines changed: 38 additions & 8 deletions

File tree

.github/docs/contribution-guide/resource.go

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"strings"
77

8+
"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts"
89
"github.com/hashicorp/terraform-plugin-framework/resource"
910
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
1011
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
@@ -16,6 +17,7 @@ import (
1617
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
1718
fooUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/foo/utils"
1819
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
20+
"k8s.io/apimachinery/pkg/api/validate"
1921

2022
"github.com/stackitcloud/stackit-sdk-go/services/foo" // Import service "foo" from the STACKIT SDK for Go
2123
"github.com/stackitcloud/stackit-sdk-go/services/foo/wait" // Import service "foo" waiters from the STACKIT SDK for Go (in case the service API has asynchronous endpoints)
@@ -32,13 +34,14 @@ var (
3234

3335
// Model is the internal model of the terraform resource
3436
type Model struct {
35-
Id types.String `tfsdk:"id"` // needed by TF
36-
ProjectId types.String `tfsdk:"project_id"`
37-
BarId types.String `tfsdk:"bar_id"`
38-
Region types.String `tfsdk:"region"`
39-
MyRequiredField types.String `tfsdk:"my_required_field"`
40-
MyOptionalField types.String `tfsdk:"my_optional_field"`
41-
MyReadOnlyField types.String `tfsdk:"my_read_only_field"`
37+
Id types.String `tfsdk:"id"` // needed by TF
38+
ProjectId types.String `tfsdk:"project_id"`
39+
BarId types.String `tfsdk:"bar_id"`
40+
Region types.String `tfsdk:"region"`
41+
MyRequiredField types.String `tfsdk:"my_required_field"`
42+
MyOptionalField types.String `tfsdk:"my_optional_field"`
43+
MyReadOnlyField types.String `tfsdk:"my_read_only_field"`
44+
Timeouts timeouts.Value `tfsdk:"timeouts"`
4245
}
4346

4447
// NewBarResource is a helper function to simplify the provider implementation.
@@ -104,7 +107,7 @@ func (r *barResource) Configure(ctx context.Context, req resource.ConfigureReque
104107
}
105108

106109
// Schema defines the schema for the resource.
107-
func (r *barResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
110+
func (r *barResource) Schema(ctx context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
108111
descriptions := map[string]string{
109112
"main": "Foo bar resource schema.",
110113
"id": "Terraform's internal resource identifier. It is structured as \"`project_id`,`bar_id`\".",
@@ -173,6 +176,7 @@ func (r *barResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *
173176
Description: descriptions["my_read_only_field"],
174177
Computed: true,
175178
},
179+
"timeouts": timeouts.AttributesAll(ctx),
176180
},
177181
}
178182
}
@@ -185,6 +189,15 @@ func (r *barResource) Create(ctx context.Context, req resource.CreateRequest, re
185189
return
186190
}
187191

192+
waiterTimeout := wait.CreateBarWaitHandler(ctx, r.client, projectId, region, resp.BarId).GetTimeout()
193+
createTimeout, diags := model.Timeouts.Create(ctx, waiterTimeout+core.DefaultTimeoutMargin)
194+
resp.Diagnostics.Append(diags...)
195+
if resp.Diagnostics.HasError() {
196+
return
197+
}
198+
ctx, cancel := context.WithTimeout(ctx, createTimeout)
199+
defer cancel()
200+
188201
ctx = core.InitProviderContext(ctx)
189202

190203
projectId := model.ProjectId.ValueString()
@@ -250,6 +263,14 @@ func (r *barResource) Read(ctx context.Context, req resource.ReadRequest, resp *
250263
return
251264
}
252265

266+
readTimeout, diags := model.Timeouts.Create(ctx, core.DefaultOperationTimeout)
267+
resp.Diagnostics.Append(diags...)
268+
if resp.Diagnostics.HasError() {
269+
return
270+
}
271+
ctx, cancel := context.WithTimeout(ctx, readTimeout)
272+
defer cancel()
273+
253274
ctx = core.InitProviderContext(ctx)
254275

255276
projectId := model.ProjectId.ValueString()
@@ -296,6 +317,15 @@ func (r *barResource) Delete(ctx context.Context, req resource.DeleteRequest, re
296317
return
297318
}
298319

320+
waiterTimeout := wait.DeleteBarWaitHandler(ctx, r.client, projectId, region, barId).GetTimeout()
321+
deleteTimeout, diags := model.Timeouts.Create(ctx, waiterTimeout+core.DefaultTimeoutMargin)
322+
resp.Diagnostics.Append(diags...)
323+
if resp.Diagnostics.HasError() {
324+
return
325+
}
326+
ctx, cancel := context.WithTimeout(ctx, deleteTimeout)
327+
defer cancel()
328+
299329
ctx = core.InitProviderContext(ctx)
300330

301331
projectId := model.ProjectId.ValueString()

0 commit comments

Comments
 (0)