forked from incentro-ecx/terraform-provider-commercelayer
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request incentro-ecx#26 from incentro-dc/feat/webhooks
feat: added webhook resource
- Loading branch information
Showing
9 changed files
with
263 additions
and
4 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,168 @@ | ||
package commercelayer | ||
|
||
import ( | ||
"context" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
commercelayer "github.com/incentro-dc/go-commercelayer-sdk/api" | ||
) | ||
|
||
func resourceWebhook() *schema.Resource { | ||
return &schema.Resource{ | ||
Description: "A webhook object is returned as part of the response body of each successful list, retrieve, " + | ||
"create or update API call to the /api/webhooks endpoint.", | ||
ReadContext: resourceWebhookReadFunc, | ||
CreateContext: resourceWebhookCreateFunc, | ||
UpdateContext: resourceWebhookUpdateFunc, | ||
DeleteContext: resourceWebhookDeleteFunc, | ||
Importer: &schema.ResourceImporter{ | ||
StateContext: schema.ImportStatePassthroughContext, | ||
}, | ||
Schema: map[string]*schema.Schema{ | ||
"id": { | ||
Description: "The webhook unique identifier", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"attributes": { | ||
Description: "Resource attributes", | ||
Type: schema.TypeList, | ||
MaxItems: 1, | ||
MinItems: 1, | ||
Required: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Description: "Unique name for the webhook.", | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Default: "webhook", | ||
}, | ||
"topic": { | ||
Description: "The identifier of the resource/event that will trigger the webhook.", | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"callback_url": { | ||
Description: "URI where the webhook subscription should send the POST request when the " + | ||
"event occurs.", | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"include_resources": { | ||
Description: "List of related resources that should be included in the webhook body.", | ||
Type: schema.TypeList, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
Optional: true, | ||
}, | ||
"reference": { | ||
Description: "A string that you can use to add any external identifier to the resource. This " + | ||
"can be useful for integrating the resource to an external system, like an ERP, a " + | ||
"marketing tool, a CRM, or whatever.", | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"reference_origin": { | ||
Description: "Any identifier of the third party system that defines the reference code", | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"metadata": { | ||
Description: "Set of key-value pairs that you can attach to the resource. This can be useful " + | ||
"for storing additional information about the resource in a structured format", | ||
Type: schema.TypeMap, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
Optional: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceWebhookReadFunc(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics { | ||
c := i.(*commercelayer.APIClient) | ||
|
||
resp, _, err := c.WebhooksApi.GETWebhooksWebhookId(ctx, d.Id()).Execute() | ||
if err != nil { | ||
return diagErr(err) | ||
} | ||
|
||
webhook, ok := resp.GetDataOk() | ||
if !ok { | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
d.SetId(webhook.GetId()) | ||
|
||
return nil | ||
} | ||
|
||
func resourceWebhookCreateFunc(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics { | ||
c := i.(*commercelayer.APIClient) | ||
|
||
attributes := d.Get("attributes").([]interface{})[0].(map[string]interface{}) | ||
|
||
webhookCreate := commercelayer.WebhookCreate{ | ||
Data: commercelayer.WebhookCreateData{ | ||
Type: webhookType, | ||
Attributes: commercelayer.POSTWebhooks201ResponseDataAttributes{ | ||
Name: stringRef(attributes["name"]), | ||
Topic: attributes["topic"].(string), | ||
CallbackUrl: attributes["callback_url"].(string), | ||
IncludeResources: stringSliceValueRef(attributes["include_resources"]), | ||
Reference: stringRef(attributes["reference"]), | ||
ReferenceOrigin: stringRef(attributes["reference_origin"]), | ||
Metadata: keyValueRef(attributes["metadata"]), | ||
}, | ||
}, | ||
} | ||
|
||
address, _, err := c.WebhooksApi.POSTWebhooks(ctx).WebhookCreate(webhookCreate).Execute() | ||
if err != nil { | ||
return diagErr(err) | ||
} | ||
|
||
d.SetId(*address.Data.Id) | ||
|
||
return nil | ||
} | ||
|
||
func resourceWebhookDeleteFunc(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics { | ||
c := i.(*commercelayer.APIClient) | ||
_, err := c.WebhooksApi.DELETEWebhooksWebhookId(ctx, d.Id()).Execute() | ||
return diag.FromErr(err) | ||
} | ||
|
||
func resourceWebhookUpdateFunc(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics { | ||
c := i.(*commercelayer.APIClient) | ||
|
||
attributes := d.Get("attributes").([]interface{})[0].(map[string]interface{}) | ||
|
||
var webhookUpdate = commercelayer.WebhookUpdate{ | ||
Data: commercelayer.WebhookUpdateData{ | ||
Type: webhookType, | ||
Id: d.Id(), | ||
Attributes: commercelayer.PATCHWebhooksWebhookId200ResponseDataAttributes{ | ||
Name: stringRef(attributes["name"]), | ||
Topic: stringRef(attributes["topic"]), | ||
CallbackUrl: stringRef(attributes["callback_url"]), | ||
IncludeResources: stringSliceValueRef(attributes["include_resources"]), | ||
Reference: stringRef(attributes["reference"]), | ||
ReferenceOrigin: stringRef(attributes["reference_origin"]), | ||
Metadata: keyValueRef(attributes["metadata"]), | ||
}, | ||
}, | ||
} | ||
|
||
_, _, err := c.WebhooksApi.PATCHWebhooksWebhookId(ctx, d.Id()).WebhookUpdate(webhookUpdate).Execute() | ||
|
||
return diag.FromErr(err) | ||
|
||
} |
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
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,42 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "commercelayer_webhook Resource - terraform-provider-commercelayer" | ||
subcategory: "" | ||
description: |- | ||
A webhook object is returned as part of the response body of each successful list, retrieve, create or update API call to the /api/webhooks endpoint. | ||
--- | ||
|
||
# commercelayer_webhook (Resource) | ||
|
||
A webhook object is returned as part of the response body of each successful list, retrieve, create or update API call to the /api/webhooks endpoint. | ||
|
||
|
||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `attributes` (Block List, Min: 1, Max: 1) Resource attributes (see [below for nested schema](#nestedblock--attributes)) | ||
|
||
### Read-Only | ||
|
||
- `id` (String) The webhook unique identifier | ||
|
||
<a id="nestedblock--attributes"></a> | ||
### Nested Schema for `attributes` | ||
|
||
Required: | ||
|
||
- `callback_url` (String) URI where the webhook subscription should send the POST request when the event occurs. | ||
- `topic` (String) The identifier of the resource/event that will trigger the webhook. | ||
|
||
Optional: | ||
|
||
- `include_resources` (List of String) List of related resources that should be included in the webhook body. | ||
- `metadata` (Map of String) Set of key-value pairs that you can attach to the resource. This can be useful for storing additional information about the resource in a structured format | ||
- `name` (String) Unique name for the webhook. | ||
- `reference` (String) A string that you can use to add any external identifier to the resource. This can be useful for integrating the resource to an external system, like an ERP, a marketing tool, a CRM, or whatever. | ||
- `reference_origin` (String) Any identifier of the third party system that defines the reference code | ||
|
||
|
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,14 @@ | ||
resource "commercelayer_webhook" "orders_create_webhook" { | ||
attributes { | ||
name = "order-fulfillment" | ||
topic = "orders.create" | ||
callback_url = "http://example.url" | ||
include_resources = [ | ||
"customer", | ||
"line_items" | ||
] | ||
metadata = { | ||
foo : "bar" | ||
} | ||
} | ||
} |