Skip to content

Commit

Permalink
add wait_until_fulfilled arg, support create timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
circa10a committed Jan 17, 2023
1 parent 9f8b2be commit 8f08a36
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ HOSTNAME=github.com
NAMESPACE=circa10a
NAME=mailform
BINARY=terraform-provider-${NAME}
VERSION=0.1
VERSION=0.2.0
OS_ARCH=darwin_amd64

default: install
Expand Down
10 changes: 10 additions & 0 deletions docs/resources/order.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ Mailform order
- `pdf_url` (String) URL of PDF to be printed and mailed by mailform.
- `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.
- `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.
- `timeouts` (Block, Optional) (see [below for nested schema](#nestedblock--timeouts))
- `to_address_2` (String) The suite or room number of the recipient of this envelope or postcard.
- `to_organization` (String) The organization or company associated with the recipient of this envelope or postcard.
- `wait_until_fulfilled` (Boolean) Wait until order is fulfilled (mailed). Default timeout is 5 days, but may be overridden using a timeouts block.
- `webhook` (String) The webhook that should receive notifications about order updates to this order.

### Read-Only
Expand All @@ -68,6 +70,14 @@ Mailform order
- `test_mode` (Boolean)
- `total` (Number)

<a id="nestedblock--timeouts"></a>
### Nested Schema for `timeouts`

Optional:

- `create` (String)


<a id="nestedatt--lineitems"></a>
### Nested Schema for `lineitems`

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/circa10a/terraform-provider-mailform
go 1.18

require (
github.com/circa10a/go-mailform v0.4.0
github.com/circa10a/go-mailform v0.5.0
github.com/hashicorp/terraform-plugin-docs v0.13.0
github.com/hashicorp/terraform-plugin-log v0.7.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.24.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkY
github.com/bgentry/speakeasy v0.1.0 h1:ByYyxL9InA1OWqxJqqp2A5pYHUrCiAL6K3J+LKSsQkY=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/boombuler/barcode v1.0.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
github.com/circa10a/go-mailform v0.4.0 h1:PVVR+Ue20qAp1oEG4R2fGX7PwSafl2NRg8mA/4JlceU=
github.com/circa10a/go-mailform v0.4.0/go.mod h1:oCX+R+o4jbjRyFYlcfnDzjt+zALo1w7Gt1DG9Tz1qGg=
github.com/circa10a/go-mailform v0.5.0 h1:omK1d+/24xhb05qQ3c6ReU9lUeDrKk4WiMwSgivNKFc=
github.com/circa10a/go-mailform v0.5.0/go.mod h1:oCX+R+o4jbjRyFYlcfnDzjt+zALo1w7Gt1DG9Tz1qGg=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down
70 changes: 69 additions & 1 deletion internal/provider/resource_mailform_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,27 @@ package provider

import (
"context"
"errors"
"fmt"
"time"

"github.com/circa10a/go-mailform"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"golang.org/x/exp/maps"
)

const (
orderStatusPollInterval = time.Minute * 30
orderFulFillmentDefaultTimeout = time.Hour * 24 * 5 // 5 days
)

var (
errOrderCancelled = errors.New("order has been cancelled")
)

var orderInputSchema = map[string]*schema.Schema{
"pdf_file": {
Description: "File path of PDF to be printed and mailed by mailform. Orders cannot be updated/deleted.",
Expand Down Expand Up @@ -207,6 +220,12 @@ var orderInputSchema = map[string]*schema.Schema{
Optional: true,
ForceNew: true,
},
"wait_until_fulfilled": {
Description: "Wait until order is fulfilled (mailed). Default timeout is 5 days, but may be overridden using a timeouts block.",
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},
// Computed
"id": {
Type: schema.TypeString,
Expand All @@ -230,6 +249,9 @@ func resourceMailformOrder() *schema.Resource {
ReadContext: orderRead,
DeleteContext: resourceMailformOrderDelete,
Schema: getOrderCreateSchema(),
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(orderFulFillmentDefaultTimeout),
},
}
}

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

d.SetId(result.Data.ID)
orderID := result.Data.ID
d.SetId(orderID)

if d.Get("wait_until_fulfilled").(bool) {
ticker := time.NewTicker(orderStatusPollInterval)
timer := time.NewTimer(d.Timeout(schema.TimeoutCreate))

// If order was cancelled, break early
order, err := client.GetOrder(orderID)
if err != nil {
tflog.Error(ctx, err.Error())
}
orderStatus := order.Data.State
if orderStatus == mailform.StatusCancelled {
return diag.FromErr(errOrderCancelled)
}

for {
select {
case <-ticker.C:
// Get order status
order, err := client.GetOrder(orderID)
if err != nil {
tflog.Error(ctx, err.Error())
}
orderStatus := order.Data.State
tflog.Debug(ctx, fmt.Sprintf("order state: %s", orderStatus))
// If order has been mailed
if orderStatus == mailform.StatusFulfilled {
return orderRead(ctx, d, m)
}
// If order was cancelled, break
if orderStatus == mailform.StatusCancelled {
return diag.FromErr(errOrderCancelled)
}
select {
case <-timer.C:
return diag.FromErr(errors.New("waiting for order to be fulfilled timed out"))
default:
break
}
case <-ctx.Done():
return diag.FromErr(errors.New("waiting for order cancelled"))
}
}

}

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

0 comments on commit 8f08a36

Please sign in to comment.