Skip to content

Commit

Permalink
Merge pull request incentro-ecx#22 from incentro-dc/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
Thomas De Meyer authored Sep 27, 2022
2 parents e1288b1 + bfc5ce1 commit d00fa69
Show file tree
Hide file tree
Showing 29 changed files with 1,083 additions and 55 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/testing.yml
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 ./...
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.DS_Store
/terraform-provider-commercelayer
/examples/**/*.tfstate*
/examples/**/*.tfvars
Expand Down
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.DEFAULT_GOAL := build
.DEFAULT_GOAL := generate build

fmt:
go fmt ./...
Expand All @@ -9,3 +9,5 @@ test:
build:
go build ./...

generate:
go generate
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ terraform {
required_providers {
commercelayer = {
version = ">= 0.0.1"
source = "incentro-dc/commercelayer"
source = "incentro/commercelayer"
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion commercelayer/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ func Provider() *schema.Provider {
},
},
ResourcesMap: map[string]*schema.Resource{
"commercelayer_address": resourceAddress(),
"commercelayer_address": resourceAddress(),
"commercelayer_merchant": resourceMerchant(),
"commercelayer_price_list": resourcePriceList(),
"commercelayer_customer_group": resourceCustomerGroup(),
},
ConfigureContextFunc: providerConfigureFunc,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,18 +142,6 @@ func resourceAddress() *schema.Resource {
},
},
},
"relationships": {
Description: "Resource relationships",
Type: schema.TypeList,
MaxItems: 1,
MinItems: 1,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
// TODO: implement geocoder relation
},
},
},
},
}
}
Expand Down
141 changes: 141 additions & 0 deletions commercelayer/resource_customer_group.go
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)
}
175 changes: 175 additions & 0 deletions commercelayer/resource_merchant.go
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)
}
Loading

0 comments on commit d00fa69

Please sign in to comment.