diff --git a/commercelayer/provider.go b/commercelayer/provider.go index 35bdc9c..3c1fd34 100644 --- a/commercelayer/provider.go +++ b/commercelayer/provider.go @@ -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, diff --git a/commercelayer/resource_addresses.go b/commercelayer/resource_address.go similarity index 100% rename from commercelayer/resource_addresses.go rename to commercelayer/resource_address.go diff --git a/commercelayer/resource_customer_groups.go b/commercelayer/resource_customer_group.go similarity index 98% rename from commercelayer/resource_customer_groups.go rename to commercelayer/resource_customer_group.go index 6a29abe..9d25cc4 100644 --- a/commercelayer/resource_customer_groups.go +++ b/commercelayer/resource_customer_group.go @@ -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, }, @@ -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"]), @@ -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) diff --git a/commercelayer/resource_merchants.go b/commercelayer/resource_merchant.go similarity index 100% rename from commercelayer/resource_merchants.go rename to commercelayer/resource_merchant.go diff --git a/commercelayer/resource_price_list.go b/commercelayer/resource_price_list.go new file mode 100644 index 0000000..17e6b31 --- /dev/null +++ b/commercelayer/resource_price_list.go @@ -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": { + 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) +} diff --git a/commercelayer/types.go b/commercelayer/types.go index ef149e0..eeb19eb 100644 --- a/commercelayer/types.go +++ b/commercelayer/types.go @@ -1,7 +1,8 @@ package commercelayer const ( - addressType = "addresses" - merchantType = "merchants" + addressType = "addresses" + merchantType = "merchants" customerGroupType = "customer_groups" + priceListType = "price_lists" ) diff --git a/examples/full/price_lists.tf b/examples/full/price_lists.tf new file mode 100644 index 0000000..01c90f3 --- /dev/null +++ b/examples/full/price_lists.tf @@ -0,0 +1,9 @@ +resource "commercelayer_price_list" "incentro_price_list" { + attributes { + name = "incentro_group" + currency_code = "EUR" + metadata = { + foo: "bar" + } + } +} \ No newline at end of file diff --git a/go.sum b/go.sum index 24f4363..62d43d3 100644 --- a/go.sum +++ b/go.sum @@ -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=