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#22 from incentro-dc/development
Development
- Loading branch information
Showing
29 changed files
with
1,083 additions
and
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
name: Run tests | ||
|
||
on: | ||
push: | ||
branches: [ "development" ] | ||
pull_request: | ||
branches: [ "development" ] | ||
|
||
jobs: | ||
unit: | ||
name: Unit Tests | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-go@v3 | ||
with: | ||
go-version: '1.18' | ||
- run: go test -v -cover ./... |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/.DS_Store | ||
/terraform-provider-commercelayer | ||
/examples/**/*.tfstate* | ||
/examples/**/*.tfvars | ||
|
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,141 @@ | ||
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 resourceCustomerGroup() *schema.Resource { | ||
return &schema.Resource{ | ||
Description: `A customer group is a resource that can be used to organize customers into groups. | ||
When you associate a customer group to a market, that market becomes private and can be accessed | ||
only by the customers belonging to the group. You can use customer groups to manage B2B customers, | ||
B2C loyalty programs, private sales, and more.`, | ||
ReadContext: resourceCustomerGroupReadFunc, | ||
CreateContext: resourceCustomerGroupCreateFunc, | ||
UpdateContext: resourceCustomerGroupUpdateFunc, | ||
DeleteContext: resourceCustomerGroupDeleteFunc, | ||
Importer: &schema.ResourceImporter{ | ||
StateContext: schema.ImportStatePassthroughContext, | ||
}, | ||
Schema: map[string]*schema.Schema{ | ||
"id": { | ||
Description: "The CustomerGroup unique identifier", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"attributes": { | ||
Description: "Resource attributes", | ||
Type: schema.TypeList, | ||
Required: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Description: "The customer group's internal name", | ||
Type: schema.TypeString, | ||
Required: 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 resourceCustomerGroupReadFunc(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics { | ||
c := i.(*commercelayer.APIClient) | ||
|
||
resp, _, err := c.CustomerGroupsApi.GETCustomerGroupsCustomerGroupId(ctx, d.Id()).Execute() | ||
if err != nil { | ||
return diagErr(err) | ||
} | ||
|
||
customerGroup, ok := resp.GetDataOk() | ||
if !ok { | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
d.SetId(customerGroup.GetId()) | ||
|
||
return nil | ||
} | ||
|
||
func resourceCustomerGroupCreateFunc(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics { | ||
c := i.(*commercelayer.APIClient) | ||
|
||
attributes := d.Get("attributes").([]interface{})[0].(map[string]interface{}) | ||
|
||
customerGroupCreate := commercelayer.CustomerGroupCreate{ | ||
Data: commercelayer.CustomerGroupCreateData{ | ||
Type: customerGroupType, | ||
Attributes: commercelayer.POSTCustomerGroups201ResponseDataAttributes{ | ||
Name: attributes["name"].(string), | ||
Reference: stringRef(attributes["reference"]), | ||
ReferenceOrigin: stringRef(attributes["reference_origin"]), | ||
Metadata: keyValueRef(attributes["metadata"]), | ||
}, | ||
}, | ||
} | ||
|
||
customerGroup, _, err := c.CustomerGroupsApi.POSTCustomerGroups(ctx).CustomerGroupCreate(customerGroupCreate).Execute() | ||
if err != nil { | ||
return diagErr(err) | ||
} | ||
|
||
d.SetId(*customerGroup.Data.Id) | ||
|
||
return nil | ||
} | ||
|
||
func resourceCustomerGroupDeleteFunc(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics { | ||
c := i.(*commercelayer.APIClient) | ||
_, err := c.CustomerGroupsApi.DELETECustomerGroupsCustomerGroupId(ctx, d.Id()).Execute() | ||
return diag.FromErr(err) | ||
} | ||
|
||
func resourceCustomerGroupUpdateFunc(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics { | ||
c := i.(*commercelayer.APIClient) | ||
|
||
attributes := d.Get("attributes").([]interface{})[0].(map[string]interface{}) | ||
|
||
var customerGroupUpdate = commercelayer.CustomerGroupUpdate{ | ||
Data: commercelayer.CustomerGroupUpdateData{ | ||
Type: customerGroupType, | ||
Id: d.Id(), | ||
Attributes: commercelayer.PATCHCustomerGroupsCustomerGroupId200ResponseDataAttributes{ | ||
Name: stringRef(attributes["name"].(string)), | ||
Reference: stringRef(attributes["reference"]), | ||
ReferenceOrigin: stringRef(attributes["reference_origin"]), | ||
Metadata: keyValueRef(attributes["metadata"]), | ||
}, | ||
}, | ||
} | ||
|
||
_, _, err := c.CustomerGroupsApi.PATCHCustomerGroupsCustomerGroupId(ctx, d.Id()).CustomerGroupUpdate(customerGroupUpdate).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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
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 resourceMerchant() *schema.Resource { | ||
return &schema.Resource{ | ||
Description: "A merchant is the fiscal representative that is selling in a specific market. Tax calculators " + | ||
"use the merchant's address (and the shipping address) to determine the tax rate for an order.", | ||
ReadContext: resourceMerchantReadFunc, | ||
CreateContext: resourceMerchantCreateFunc, | ||
UpdateContext: resourceMerchantUpdateFunc, | ||
DeleteContext: resourceMerchantDeleteFunc, | ||
Importer: &schema.ResourceImporter{ | ||
StateContext: schema.ImportStatePassthroughContext, | ||
}, | ||
Schema: map[string]*schema.Schema{ | ||
"id": { | ||
Description: "The merchant 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: "The merchant's internal name", | ||
Type: schema.TypeString, | ||
Required: 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, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"relationships": { | ||
Description: "Resource relationships", | ||
Type: schema.TypeList, | ||
MaxItems: 1, | ||
MinItems: 1, | ||
Required: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"address": { | ||
Description: "The related address", | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceMerchantReadFunc(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics { | ||
c := i.(*commercelayer.APIClient) | ||
|
||
resp, _, err := c.MerchantsApi.GETMerchantsMerchantId(ctx, d.Id()).Execute() | ||
if err != nil { | ||
return diagErr(err) | ||
} | ||
|
||
merchant, ok := resp.GetDataOk() | ||
if !ok { | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
d.SetId(merchant.GetId()) | ||
|
||
return nil | ||
} | ||
|
||
func resourceMerchantCreateFunc(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics { | ||
c := i.(*commercelayer.APIClient) | ||
|
||
attributes := d.Get("attributes").([]any)[0].(map[string]any) | ||
relationships := d.Get("relationships").([]any)[0].(map[string]any) | ||
|
||
merchantCreate := commercelayer.MerchantCreate{ | ||
Data: commercelayer.MerchantCreateData{ | ||
Type: merchantType, | ||
Attributes: commercelayer.POSTMerchants201ResponseDataAttributes{ | ||
Name: attributes["name"].(string), | ||
Reference: stringRef(attributes["reference"]), | ||
ReferenceOrigin: stringRef(attributes["reference_origin"]), | ||
Metadata: keyValueRef(attributes["metadata"]), | ||
}, | ||
Relationships: &commercelayer.MerchantCreateDataRelationships{ | ||
Address: commercelayer.BingGeocoderDataRelationshipsAddresses{ | ||
Data: commercelayer.BingGeocoderDataRelationshipsAddressesData{ | ||
Type: stringRef(addressType), | ||
Id: stringRef(relationships["address"].(string)), | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
merchant, _, err := c.MerchantsApi.POSTMerchants(ctx).MerchantCreate(merchantCreate).Execute() | ||
if err != nil { | ||
return diagErr(err) | ||
} | ||
|
||
d.SetId(*merchant.Data.Id) | ||
|
||
return nil | ||
} | ||
|
||
func resourceMerchantDeleteFunc(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics { | ||
c := i.(*commercelayer.APIClient) | ||
_, err := c.MerchantsApi.DELETEMerchantsMerchantId(ctx, d.Id()).Execute() | ||
return diag.FromErr(err) | ||
} | ||
|
||
func resourceMerchantUpdateFunc(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics { | ||
c := i.(*commercelayer.APIClient) | ||
|
||
attributes := d.Get("attributes").([]any)[0].(map[string]any) | ||
relationships := d.Get("relationships").([]any)[0].(map[string]any) | ||
|
||
var merchantUpdate = commercelayer.MerchantUpdate{ | ||
Data: commercelayer.MerchantUpdateData{ | ||
Type: merchantType, | ||
Id: d.Id(), | ||
Attributes: commercelayer.PATCHMerchantsMerchantId200ResponseDataAttributes{ | ||
Name: stringRef(attributes["name"].(string)), | ||
Reference: stringRef(attributes["reference"]), | ||
ReferenceOrigin: stringRef(attributes["reference_origin"]), | ||
Metadata: keyValueRef(attributes["metadata"]), | ||
}, | ||
Relationships: &commercelayer.MerchantUpdateDataRelationships{ | ||
Address: &commercelayer.BingGeocoderDataRelationshipsAddresses{ | ||
Data: commercelayer.BingGeocoderDataRelationshipsAddressesData{ | ||
Type: stringRef(addressType), | ||
Id: stringRef(relationships["address"].(string)), | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
_, _, err := c.MerchantsApi.PATCHMerchantsMerchantId(ctx, d.Id()).MerchantUpdate(merchantUpdate).Execute() | ||
|
||
return diag.FromErr(err) | ||
} |
Oops, something went wrong.