Skip to content

Commit

Permalink
feat: added resource_inventory_return_location.go and resource_invent…
Browse files Browse the repository at this point in the history
…ory_stock_location.go
  • Loading branch information
Thomas De Meyer committed Nov 24, 2022
1 parent 60a50d3 commit f422868
Show file tree
Hide file tree
Showing 61 changed files with 2,712 additions and 38 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 15 additions & 13 deletions commercelayer/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,21 @@ var baseSchema = map[string]*schema.Schema{
}

var baseResourceMap = map[string]*schema.Resource{
"commercelayer_address": resourceAddress(),
"commercelayer_merchant": resourceMerchant(),
"commercelayer_price_list": resourcePriceList(),
"commercelayer_customer_group": resourceCustomerGroup(),
"commercelayer_webhook": resourceWebhook(),
"commercelayer_external_gateway": resourceExternalGateway(),
"commercelayer_external_tax_calculator": resourceExternalTaxCalculator(),
"commercelayer_market": resourceMarket(),
"commercelayer_inventory_model": resourceInventoryModel(),
"commercelayer_shipping_method": resourceShippingMethod(),
"commercelayer_shipping_zone": resourceShippingZone(),
"commercelayer_shipping_category": resourceShippingCategory(),
"commercelayer_stock_location": resourceStockLocation(),
"commercelayer_address": resourceAddress(),
"commercelayer_merchant": resourceMerchant(),
"commercelayer_price_list": resourcePriceList(),
"commercelayer_customer_group": resourceCustomerGroup(),
"commercelayer_webhook": resourceWebhook(),
"commercelayer_external_gateway": resourceExternalGateway(),
"commercelayer_external_tax_calculator": resourceExternalTaxCalculator(),
"commercelayer_market": resourceMarket(),
"commercelayer_inventory_model": resourceInventoryModel(),
"commercelayer_shipping_method": resourceShippingMethod(),
"commercelayer_shipping_zone": resourceShippingZone(),
"commercelayer_shipping_category": resourceShippingCategory(),
"commercelayer_stock_location": resourceStockLocation(),
"commercelayer_inventory_return_location": resourceInventoryReturnLocation(),
"commercelayer_inventory_stock_location": resourceInventoryStockLocation(),
}

type Configuration struct {
Expand Down
204 changes: 204 additions & 0 deletions commercelayer/resource_inventory_return_location.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
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 resourceInventoryReturnLocation() *schema.Resource {
return &schema.Resource{
Description: "Inventory return locations build a hierarchy of stock locations within an inventory " +
"model, determining the available options for the returns. In the case a SKU is available in more stock " +
"locations, it gets returned to those with the highest priority.",
ReadContext: resourceInventoryReturnLocationReadFunc,
CreateContext: resourceInventoryReturnLocationCreateFunc,
UpdateContext: resourceInventoryReturnLocationUpdateFunc,
DeleteContext: resourceInventoryReturnLocationDeleteFunc,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"id": {
Description: "The inventory return location unique identifier",
Type: schema.TypeString,
Computed: true,
},
"type": {
Description: "The resource type",
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{
"priority": {
Description: "The inventory model's internal name.",
Type: schema.TypeInt,
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 " +
"InventoryReturnLocationing 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{
"stock_location_id": {
Description: "The associated stock location id.",
Type: schema.TypeString,
Required: true,
},
"inventory_model_id": {
Description: "The associated inventory model id.",
Type: schema.TypeString,
Required: true,
},
},
},
},
},
}
}

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

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

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

d.SetId(inventoryModel.GetId())

return nil
}

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

attributes := nestedMap(d.Get("attributes"))
relationships := nestedMap(d.Get("relationships"))

inventoryModelCreate := commercelayer.InventoryReturnLocationCreate{
Data: commercelayer.InventoryReturnLocationCreateData{
Type: inventoryReturnLocationsType,
Attributes: commercelayer.POSTInventoryReturnLocations201ResponseDataAttributes{
Priority: int32(attributes["priority"].(int)),
Reference: stringRef(attributes["reference"]),
ReferenceOrigin: stringRef(attributes["reference_origin"]),
Metadata: keyValueRef(attributes["metadata"]),
},
Relationships: &commercelayer.InventoryReturnLocationCreateDataRelationships{
StockLocation: commercelayer.DeliveryLeadTimeDataRelationshipsStockLocation{
Data: commercelayer.DeliveryLeadTimeDataRelationshipsStockLocationData{
Type: stringRef(stockLocationType),
Id: stringRef(relationships["stock_location_id"]),
},
},
InventoryModel: commercelayer.InventoryReturnLocationDataRelationshipsInventoryModel{
Data: commercelayer.InventoryReturnLocationDataRelationshipsInventoryModelData{
Type: stringRef(inventoryModelType),
Id: stringRef(relationships["inventory_model_id"]),
},
},
},
},
}

err := d.Set("type", inventoryReturnLocationsType)
if err != nil {
return diagErr(err)
}

inventoryModel, _, err := c.InventoryReturnLocationsApi.POSTInventoryReturnLocations(ctx).InventoryReturnLocationCreate(inventoryModelCreate).Execute()
if err != nil {
return diagErr(err)
}

d.SetId(*inventoryModel.Data.Id)

return nil
}

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

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

attributes := nestedMap(d.Get("attributes"))
relationships := nestedMap(d.Get("relationships"))

var inventoryModelUpdate = commercelayer.InventoryReturnLocationUpdate{
Data: commercelayer.InventoryReturnLocationUpdateData{
Type: inventoryReturnLocationsType,
Id: d.Id(),
Attributes: commercelayer.PATCHInventoryReturnLocationsInventoryReturnLocationId200ResponseDataAttributes{
Priority: intToInt32Ref(attributes["priority"]),
Reference: stringRef(attributes["reference"]),
ReferenceOrigin: stringRef(attributes["reference_origin"]),
Metadata: keyValueRef(attributes["metadata"]),
},
Relationships: &commercelayer.InventoryReturnLocationDataRelationships{
StockLocation: &commercelayer.DeliveryLeadTimeDataRelationshipsStockLocation{
Data: commercelayer.DeliveryLeadTimeDataRelationshipsStockLocationData{
Type: stringRef(stockLocationType),
Id: stringRef(relationships["stock_location_id"]),
},
},
InventoryModel: &commercelayer.InventoryReturnLocationDataRelationshipsInventoryModel{
Data: commercelayer.InventoryReturnLocationDataRelationshipsInventoryModelData{
Type: stringRef(inventoryModelType),
Id: stringRef(relationships["inventory_model_id"]),
},
},
},
},
}

_, _, err := c.InventoryReturnLocationsApi.PATCHInventoryReturnLocationsInventoryReturnLocationId(ctx, d.Id()).
InventoryReturnLocationUpdate(inventoryModelUpdate).Execute()

return diag.FromErr(err)
}
Loading

0 comments on commit f422868

Please sign in to comment.