Skip to content

Commit

Permalink
Merge pull request incentro-ecx#49 from incentro-dc/feat/shipping-met…
Browse files Browse the repository at this point in the history
…hod-resource

Feat/shipping method resource
  • Loading branch information
Thomas De Meyer authored Nov 11, 2022
2 parents 31a6564 + db9441b commit 60a50d3
Show file tree
Hide file tree
Showing 60 changed files with 3,038 additions and 19 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.

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

4 changes: 4 additions & 0 deletions commercelayer/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ var baseResourceMap = map[string]*schema.Resource{
"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(),
}

type Configuration struct {
Expand Down
152 changes: 152 additions & 0 deletions commercelayer/resource_shipping_category.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
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 resourceShippingCategory() *schema.Resource {
return &schema.Resource{
Description: "Shipping categories determine which shipping methods are available for the associated " +
"SKU's. Unless the selected inventory model strategy is no_split, if an order contains line items " +
"belonging to more than one shipping category it is split into more shipments.",
ReadContext: resourceShippingCategoryReadFunc,
CreateContext: resourceShippingCategoryCreateFunc,
UpdateContext: resourceShippingCategoryUpdateFunc,
DeleteContext: resourceShippingCategoryDeleteFunc,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"id": {
Description: "The shipping category 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{
"name": {
Description: "The shipping category'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 resourceShippingCategoryReadFunc(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics {
c := i.(*commercelayer.APIClient)

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

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

d.SetId(shippingCategory.GetId())

return nil
}

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

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

shippingCategoryCreate := commercelayer.ShippingCategoryCreate{
Data: commercelayer.ShippingCategoryCreateData{
Type: shippingCategoryType,
Attributes: commercelayer.POSTShippingCategories201ResponseDataAttributes{
Name: attributes["name"].(string),
Reference: stringRef(attributes["reference"]),
ReferenceOrigin: stringRef(attributes["reference_origin"]),
Metadata: keyValueRef(attributes["metadata"]),
},
},
}

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

shippingCategory, _, err := c.ShippingCategoriesApi.POSTShippingCategories(ctx).ShippingCategoryCreate(shippingCategoryCreate).Execute()
if err != nil {
return diagErr(err)
}

d.SetId(*shippingCategory.Data.Id)

return nil
}

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

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

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

var shippingCategoryUpdate = commercelayer.ShippingCategoryUpdate{
Data: commercelayer.ShippingCategoryUpdateData{
Type: shippingCategoryType,
Id: d.Id(),
Attributes: commercelayer.PATCHShippingCategoriesShippingCategoryId200ResponseDataAttributes{
Name: stringRef(attributes["name"]),
Reference: stringRef(attributes["reference"]),
ReferenceOrigin: stringRef(attributes["reference_origin"]),
Metadata: keyValueRef(attributes["metadata"]),
},
},
}

_, _, err := c.ShippingCategoriesApi.PATCHShippingCategoriesShippingCategoryId(ctx, d.Id()).ShippingCategoryUpdate(shippingCategoryUpdate).Execute()

return diag.FromErr(err)
}
85 changes: 85 additions & 0 deletions commercelayer/resource_shipping_category_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package commercelayer

import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
commercelayer "github.com/incentro-dc/go-commercelayer-sdk/api"
"net/http"
)

func testAccCheckShippingCategoryDestroy(s *terraform.State) error {
client := testAccProviderCommercelayer.Meta().(*commercelayer.APIClient)

for _, rs := range s.RootModule().Resources {
if rs.Type == "commercelayer_shipping_category" {
err := retryRemoval(10, func() (*http.Response, error) {
_, resp, err := client.ShippingCategoriesApi.GETShippingCategoriesShippingCategoryId(context.Background(), rs.Primary.ID).
Execute()
return resp, err
})
if err != nil {
return err
}
}

}
return nil
}

func (s *AcceptanceSuite) TestAccShippingCategory_basic() {
resourceName := "commercelayer_shipping_category.incentro_shipping_category"

resource.Test(s.T(), resource.TestCase{
PreCheck: func() {
testAccPreCheck(s)
},
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckShippingCategoryDestroy,
Steps: []resource.TestStep{
{
Config: testAccShippingCategoryCreate(resourceName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "type", shippingCategoryType),
resource.TestCheckResourceAttr(resourceName, "attributes.0.name", "Incentro Shipping Category"),
resource.TestCheckResourceAttr(resourceName, "attributes.0.metadata.foo", "bar"),
),
},
{
Config: testAccShippingCategoryUpdate(resourceName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "attributes.0.name", "Incentro Shipping Category Updated"),
resource.TestCheckResourceAttr(resourceName, "attributes.0.metadata.bar", "foo"),
),
},
},
})
}

func testAccShippingCategoryCreate(testName string) string {
return hclTemplate(`
resource "commercelayer_shipping_category" "incentro_shipping_category" {
attributes {
name = "Incentro Shipping Category"
metadata = {
foo : "bar"
testName: "{{.testName}}"
}
}
}
`, map[string]any{"testName": testName})
}

func testAccShippingCategoryUpdate(testName string) string {
return hclTemplate(`
resource "commercelayer_shipping_category" "incentro_shipping_category" {
attributes {
name = "Incentro Shipping Category Updated"
metadata = {
bar : "foo"
testName: "{{.testName}}"
}
}
}
`, map[string]any{"testName": testName})
}
Loading

0 comments on commit 60a50d3

Please sign in to comment.