Skip to content

Commit

Permalink
Merge pull request #4 from incentro-dc/feature/merchant-resource
Browse files Browse the repository at this point in the history
feature: added basic merchant resource
  • Loading branch information
incentrotolgaakyazi authored Sep 13, 2022
2 parents e1288b1 + 40702c6 commit b31cbcc
Show file tree
Hide file tree
Showing 10 changed files with 199 additions and 23 deletions.
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ test:
go test -v ./...

build:
go build ./...

go build ./...
3 changes: 2 additions & 1 deletion commercelayer/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ func Provider() *schema.Provider {
},
},
ResourcesMap: map[string]*schema.Resource{
"commercelayer_address": resourceAddress(),
"commercelayer_address": resourceAddress(),
"commercelayer_merchant": resourceMerchant(),
},
ConfigureContextFunc: providerConfigureFunc,
}
Expand Down
12 changes: 0 additions & 12 deletions commercelayer/resource_addresses.go
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
175 changes: 175 additions & 0 deletions commercelayer/resource_merchants.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)
}
3 changes: 2 additions & 1 deletion commercelayer/types.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package commercelayer

const (
addressType = "addresses"
addressType = "addresses"
merchantType = "merchants"
)
6 changes: 1 addition & 5 deletions examples/full/addresses.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
resource "commercelayer_address" "incentro-address" {
resource "commercelayer_address" "incentro_address" {
attributes {
business = true
company = "Incentro"
Expand All @@ -12,8 +12,4 @@ resource "commercelayer_address" "incentro-address" {
foo: "bar"
}
}

relationships {

}
}
12 changes: 12 additions & 0 deletions examples/full/merchant.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
resource "commercelayer_merchant" "incentro_merchant" {
attributes {
name = "Incentro Merchant"
metadata = {
foo: "bar"
}
}

relationships {
address = commercelayer_address.incentro_address.id
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/hashicorp/terraform-plugin-sdk/v2 v2.7.1
github.com/incentro-dc/go-commercelayer-sdk v0.0.0-20220708125716-60e688b00bc9
github.com/incentro-dc/go-commercelayer-sdk v0.0.0-20220909135040-a967fe9433aa
golang.org/x/oauth2 v0.0.0-20220630143837-2104d58473e0
)

Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/incentro-dc/go-commercelayer-sdk v0.0.0-20220708125716-60e688b00bc9 h1:5eEVLpTxW2FeIbwYkXzMayek9KPPrPJBmUBu067krl4=
github.com/incentro-dc/go-commercelayer-sdk v0.0.0-20220708125716-60e688b00bc9/go.mod h1:7o1L5RRkJ6Fw04+Hvjqt/dCDonYA+it1Leug5rtE+/s=
github.com/incentro-dc/go-commercelayer-sdk v0.0.0-20220708134853-5c71991be907 h1:YGbTuJ2+Xczz6qnWBdOH85OgX12nVHn4sxQvm6ohCsQ=
github.com/incentro-dc/go-commercelayer-sdk v0.0.0-20220708134853-5c71991be907/go.mod h1:7o1L5RRkJ6Fw04+Hvjqt/dCDonYA+it1Leug5rtE+/s=
github.com/incentro-dc/go-commercelayer-sdk v0.0.0-20220909135040-a967fe9433aa h1:1LNePHoMZhlXGbxii+nmS8ke4eMPuzTxz3JIk4XZCuM=
github.com/incentro-dc/go-commercelayer-sdk v0.0.0-20220909135040-a967fe9433aa/go.mod h1:7o1L5RRkJ6Fw04+Hvjqt/dCDonYA+it1Leug5rtE+/s=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
github.com/jhump/protoreflect v1.6.0 h1:h5jfMVslIg6l29nsMs0D8Wj17RDVdNYti0vDN/PZZoE=
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/incentro-dc/terraform-provider-commercelayer/commercelayer"
"log"
)

func main() {
var debugMode bool

Expand Down

0 comments on commit b31cbcc

Please sign in to comment.