Skip to content
This repository has been archived by the owner on Nov 22, 2024. It is now read-only.

Feature/price list resource #17

Merged
merged 3 commits into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions commercelayer/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ func Provider() *schema.Provider {
},
},
ResourcesMap: map[string]*schema.Resource{
"commercelayer_address": resourceAddress(),
"commercelayer_merchant": resourceMerchant(),
"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 @@ -33,7 +33,7 @@ func resourceCustomerGroup() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Description: "The Customer Group's internal name",
Description: "The customer group's internal name",
Type: schema.TypeString,
Required: true,
},
Expand Down Expand Up @@ -125,7 +125,7 @@ func resourceCustomerGroupUpdateFunc(ctx context.Context, d *schema.ResourceData
var customerGroupUpdate = commercelayer.CustomerGroupUpdate{
Data: commercelayer.CustomerGroupUpdateData{
Type: customerGroupType,
Id: d.Id(),
Id: d.Id(),
Attributes: commercelayer.PATCHCustomerGroupsCustomerGroupId200ResponseDataAttributes{
Name: stringRef(attributes["name"].(string)),
Reference: stringRef(attributes["reference"]),
Expand All @@ -134,8 +134,7 @@ func resourceCustomerGroupUpdateFunc(ctx context.Context, d *schema.ResourceData
},
},
}



_, _, err := c.CustomerGroupsApi.PATCHCustomerGroupsCustomerGroupId(ctx, d.Id()).CustomerGroupUpdate(customerGroupUpdate).Execute()

return diag.FromErr(err)
Expand Down
157 changes: 157 additions & 0 deletions commercelayer/resource_price_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
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 resourcePriceList() *schema.Resource {
return &schema.Resource{
Description: `Price lists are collections of SKU prices,
defined by currency and market. When a list of SKUs is fetched,
only SKUs with a price defined in the market's price list and at least
a stock item in one of the market stock locations will be returned.
A user can create price lists to manage international business or B2B/B2C models.`,
ReadContext: resourcePriceListReadFunc,
CreateContext: resourcePriceListCreateFunc,
UpdateContext: resourcePriceListUpdateFunc,
DeleteContext: resourcePriceListDeleteFunc,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"id": {
Description: "The PriceList 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 price list's internal name",
Type: schema.TypeString,
Required: true,
},
"currency_code": {
Description: "The international 3-letter currency code as defined by the ISO 4217 standard.",
Type: schema.TypeString,
Required: true,
},
"tax_included": {
demeyerthom marked this conversation as resolved.
Show resolved Hide resolved
Description: "Indicates if the associated prices include taxes.",
Type: schema.TypeBool,
Required: true,
Default: 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 resourcePriceListReadFunc(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics {
c := i.(*commercelayer.APIClient)

resp, _, err := c.PriceListsApi.GETPriceListsPriceListId(ctx, d.Id()).Execute()
if err != nil {
return diagErr(err)
}

priceList, ok := resp.GetDataOk()
if !ok {
d.SetId("")
return nil
}

d.SetId(priceList.GetId())

return nil
}

func resourcePriceListCreateFunc(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics {
c := i.(*commercelayer.APIClient)

attributes := d.Get("attributes").([]interface{})[0].(map[string]interface{})

priceListCreate := commercelayer.PriceListCreate{
Data: commercelayer.PriceListCreateData{
Type: priceListType,
Attributes: commercelayer.POSTPriceLists201ResponseDataAttributes{
Name: attributes["name"].(string),
CurrencyCode: attributes["currency_code"].(string),
TaxIncluded: boolRef(attributes["tax_included"]),
Reference: stringRef(attributes["reference"]),
ReferenceOrigin: stringRef(attributes["reference_origin"]),
Metadata: keyValueRef(attributes["metadata"]),
},
},
}

priceList, _, err := c.PriceListsApi.POSTPriceLists(ctx).PriceListCreate(priceListCreate).Execute()
if err != nil {
return diagErr(err)
}

d.SetId(*priceList.Data.Id)

return nil
}

func resourcePriceListDeleteFunc(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics {
c := i.(*commercelayer.APIClient)
_, err := c.PriceListsApi.DELETEPriceListsPriceListId(ctx, d.Id()).Execute()
return diag.FromErr(err)
}

func resourcePriceListUpdateFunc(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics {
c := i.(*commercelayer.APIClient)

attributes := d.Get("attributes").([]interface{})[0].(map[string]interface{})

var PriceListUpdate = commercelayer.PriceListUpdate{
Data: commercelayer.PriceListUpdateData{
Type: priceListType,
Id: d.Id(),
Attributes: commercelayer.PATCHPriceListsPriceListId200ResponseDataAttributes{
Name: stringRef(attributes["name"].(string)),
CurrencyCode: stringRef(attributes["currency_code"].(string)),
TaxIncluded: boolRef(attributes["tax_included"]),
Reference: stringRef(attributes["reference"]),
ReferenceOrigin: stringRef(attributes["reference_origin"]),
Metadata: keyValueRef(attributes["metadata"]),
},
},
}

_, _, err := c.PriceListsApi.PATCHPriceListsPriceListId(ctx, d.Id()).PriceListUpdate(PriceListUpdate).Execute()

return diag.FromErr(err)
}
5 changes: 3 additions & 2 deletions commercelayer/types.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package commercelayer

const (
addressType = "addresses"
merchantType = "merchants"
addressType = "addresses"
merchantType = "merchants"
customerGroupType = "customer_groups"
priceListType = "price_lists"
)
9 changes: 9 additions & 0 deletions examples/full/price_lists.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
resource "commercelayer_price_list" "incentro_price_list" {
attributes {
name = "incentro_group"
currency_code = "EUR"
metadata = {
foo: "bar"
}
}
}
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,6 @@ github.com/huandu/xstrings v1.3.2/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
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