Skip to content

Commit 8f08a36

Browse files
committed
add wait_until_fulfilled arg, support create timeout
1 parent 9f8b2be commit 8f08a36

File tree

5 files changed

+83
-5
lines changed

5 files changed

+83
-5
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ HOSTNAME=github.com
33
NAMESPACE=circa10a
44
NAME=mailform
55
BINARY=terraform-provider-${NAME}
6-
VERSION=0.1
6+
VERSION=0.2.0
77
OS_ARCH=darwin_amd64
88

99
default: install

docs/resources/order.md

+10
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ Mailform order
4949
- `pdf_url` (String) URL of PDF to be printed and mailed by mailform.
5050
- `simplex` (Boolean) True if the document should be printed one page to a sheet, false if the document can be printed on both sides of a sheet.
5151
- `stamp` (Boolean) True if the document MUST use a real postage stamp, false if it is acceptable to mail the document using metered postage or an imprint.
52+
- `timeouts` (Block, Optional) (see [below for nested schema](#nestedblock--timeouts))
5253
- `to_address_2` (String) The suite or room number of the recipient of this envelope or postcard.
5354
- `to_organization` (String) The organization or company associated with the recipient of this envelope or postcard.
55+
- `wait_until_fulfilled` (Boolean) Wait until order is fulfilled (mailed). Default timeout is 5 days, but may be overridden using a timeouts block.
5456
- `webhook` (String) The webhook that should receive notifications about order updates to this order.
5557

5658
### Read-Only
@@ -68,6 +70,14 @@ Mailform order
6870
- `test_mode` (Boolean)
6971
- `total` (Number)
7072

73+
<a id="nestedblock--timeouts"></a>
74+
### Nested Schema for `timeouts`
75+
76+
Optional:
77+
78+
- `create` (String)
79+
80+
7181
<a id="nestedatt--lineitems"></a>
7282
### Nested Schema for `lineitems`
7383

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/circa10a/terraform-provider-mailform
33
go 1.18
44

55
require (
6-
github.com/circa10a/go-mailform v0.4.0
6+
github.com/circa10a/go-mailform v0.5.0
77
github.com/hashicorp/terraform-plugin-docs v0.13.0
88
github.com/hashicorp/terraform-plugin-log v0.7.0
99
github.com/hashicorp/terraform-plugin-sdk/v2 v2.24.1

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkY
2929
github.com/bgentry/speakeasy v0.1.0 h1:ByYyxL9InA1OWqxJqqp2A5pYHUrCiAL6K3J+LKSsQkY=
3030
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
3131
github.com/boombuler/barcode v1.0.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
32-
github.com/circa10a/go-mailform v0.4.0 h1:PVVR+Ue20qAp1oEG4R2fGX7PwSafl2NRg8mA/4JlceU=
33-
github.com/circa10a/go-mailform v0.4.0/go.mod h1:oCX+R+o4jbjRyFYlcfnDzjt+zALo1w7Gt1DG9Tz1qGg=
32+
github.com/circa10a/go-mailform v0.5.0 h1:omK1d+/24xhb05qQ3c6ReU9lUeDrKk4WiMwSgivNKFc=
33+
github.com/circa10a/go-mailform v0.5.0/go.mod h1:oCX+R+o4jbjRyFYlcfnDzjt+zALo1w7Gt1DG9Tz1qGg=
3434
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
3535
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3636
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=

internal/provider/resource_mailform_order.go

+69-1
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,27 @@ package provider
22

33
import (
44
"context"
5+
"errors"
6+
"fmt"
7+
"time"
58

69
"github.com/circa10a/go-mailform"
10+
"github.com/hashicorp/terraform-plugin-log/tflog"
711
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
812
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
913
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1014
"golang.org/x/exp/maps"
1115
)
1216

17+
const (
18+
orderStatusPollInterval = time.Minute * 30
19+
orderFulFillmentDefaultTimeout = time.Hour * 24 * 5 // 5 days
20+
)
21+
22+
var (
23+
errOrderCancelled = errors.New("order has been cancelled")
24+
)
25+
1326
var orderInputSchema = map[string]*schema.Schema{
1427
"pdf_file": {
1528
Description: "File path of PDF to be printed and mailed by mailform. Orders cannot be updated/deleted.",
@@ -207,6 +220,12 @@ var orderInputSchema = map[string]*schema.Schema{
207220
Optional: true,
208221
ForceNew: true,
209222
},
223+
"wait_until_fulfilled": {
224+
Description: "Wait until order is fulfilled (mailed). Default timeout is 5 days, but may be overridden using a timeouts block.",
225+
Type: schema.TypeBool,
226+
Optional: true,
227+
ForceNew: true,
228+
},
210229
// Computed
211230
"id": {
212231
Type: schema.TypeString,
@@ -230,6 +249,9 @@ func resourceMailformOrder() *schema.Resource {
230249
ReadContext: orderRead,
231250
DeleteContext: resourceMailformOrderDelete,
232251
Schema: getOrderCreateSchema(),
252+
Timeouts: &schema.ResourceTimeout{
253+
Create: schema.DefaultTimeout(orderFulFillmentDefaultTimeout),
254+
},
233255
}
234256
}
235257

@@ -276,7 +298,53 @@ func resourceMailformOrderCreate(ctx context.Context, d *schema.ResourceData, m
276298
return diag.FromErr(err)
277299
}
278300

279-
d.SetId(result.Data.ID)
301+
orderID := result.Data.ID
302+
d.SetId(orderID)
303+
304+
if d.Get("wait_until_fulfilled").(bool) {
305+
ticker := time.NewTicker(orderStatusPollInterval)
306+
timer := time.NewTimer(d.Timeout(schema.TimeoutCreate))
307+
308+
// If order was cancelled, break early
309+
order, err := client.GetOrder(orderID)
310+
if err != nil {
311+
tflog.Error(ctx, err.Error())
312+
}
313+
orderStatus := order.Data.State
314+
if orderStatus == mailform.StatusCancelled {
315+
return diag.FromErr(errOrderCancelled)
316+
}
317+
318+
for {
319+
select {
320+
case <-ticker.C:
321+
// Get order status
322+
order, err := client.GetOrder(orderID)
323+
if err != nil {
324+
tflog.Error(ctx, err.Error())
325+
}
326+
orderStatus := order.Data.State
327+
tflog.Debug(ctx, fmt.Sprintf("order state: %s", orderStatus))
328+
// If order has been mailed
329+
if orderStatus == mailform.StatusFulfilled {
330+
return orderRead(ctx, d, m)
331+
}
332+
// If order was cancelled, break
333+
if orderStatus == mailform.StatusCancelled {
334+
return diag.FromErr(errOrderCancelled)
335+
}
336+
select {
337+
case <-timer.C:
338+
return diag.FromErr(errors.New("waiting for order to be fulfilled timed out"))
339+
default:
340+
break
341+
}
342+
case <-ctx.Done():
343+
return diag.FromErr(errors.New("waiting for order cancelled"))
344+
}
345+
}
346+
347+
}
280348

281349
// Set computed fields in state. Saves alot of copy paste by just running an extra GET after creating the order
282350
return orderRead(ctx, d, m)

0 commit comments

Comments
 (0)