forked from incentro-ecx/terraform-provider-commercelayer
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request incentro-ecx#49 from incentro-dc/feat/shipping-met…
…hod-resource Feat/shipping method resource
- Loading branch information
Showing
60 changed files
with
3,038 additions
and
19 deletions.
There are no files selected for viewing
10 changes: 6 additions & 4 deletions
10
...urations/run_resource_webhook_test_go.xml → ...un_resource_shipping_category_test_go.xml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
10 changes: 6 additions & 4 deletions
10
.../run_resource_inventory_model_test_go.xml → .../run_resource_shipping_method_test_go.xml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
10 changes: 6 additions & 4 deletions
10
...gurations/run_resource_market_test_go.xml → ...s/run_resource_stock_location_test_go.xml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) | ||
} |
Oops, something went wrong.