From e1941629726d8fc902d7e9453925410be58bf975 Mon Sep 17 00:00:00 2001 From: Thomas De Meyer Date: Fri, 23 Aug 2024 08:49:04 +0200 Subject: [PATCH] feat: fixed len issues with business unit settings --- internal/resources/project/model.go | 2 +- internal/resources/project/resource.go | 1 - internal/resources/project/resource_test.go | 123 ++++++++++++++++---- 3 files changed, 103 insertions(+), 23 deletions(-) diff --git a/internal/resources/project/model.go b/internal/resources/project/model.go index 75d9fa8f..b06638a1 100644 --- a/internal/resources/project/model.go +++ b/internal/resources/project/model.go @@ -342,7 +342,7 @@ func (p *Project) updateActions(plan Project) (platform.ProjectUpdate, error) { }, ) //TODO: should set associate role to nil, but that is not currently supported in the SDK - } else { + } else if len(p.BusinessUnits) != 0 && len(plan.BusinessUnits) != 0 { if !p.BusinessUnits[0].MyBusinessUnitStatusOnCreation.Equal(plan.BusinessUnits[0].MyBusinessUnitStatusOnCreation) { result.Actions = append(result.Actions, platform.ProjectChangeBusinessUnitStatusOnCreationAction{ diff --git a/internal/resources/project/resource.go b/internal/resources/project/resource.go index 937696e9..03465a59 100644 --- a/internal/resources/project/resource.go +++ b/internal/resources/project/resource.go @@ -234,7 +234,6 @@ func (r *projectResource) Schema(_ context.Context, _ resource.SchemaRequest, re "for the shippingRateInput on the cart. The keys are checked for uniqueness and the request is " + "rejected if keys are not unique", Validators: []validator.List{ - listvalidator.SizeAtMost(1), customvalidator.RequireValueValidator( "CartClassification", path.MatchRoot("shipping_rate_input_type"), diff --git a/internal/resources/project/resource_test.go b/internal/resources/project/resource_test.go index c89baa70..3b975f93 100644 --- a/internal/resources/project/resource_test.go +++ b/internal/resources/project/resource_test.go @@ -74,7 +74,7 @@ func TestAccProjectCreate_basic(t *testing.T) { assert.EqualValues(t, result.Languages, []string{"nl", "de", "en", "en-US"}) assert.EqualValues(t, result.Currencies, []string{"EUR", "USD"}) assert.Equal(t, *result.Carts.DeleteDaysAfterLastModification, 7) - assert.Equal(t, result.ShippingRateInputType, platform.CartValueType(platform.CartValueType{})) + assert.Equal(t, result.ShippingRateInputType, platform.CartValueType{}) return nil }, ), @@ -142,27 +142,43 @@ func TestAccProjectCreate_basic(t *testing.T) { resourceName, "carts.0.delete_days_after_last_modification"), ), }, - // Running this step again so project settings match what later shipping_zone_rate_test will need - /* - { - Config: testAccProjectConfig("acctest_project_settings"), - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(resourceName, "name", "Test this thing"), - resource.TestCheckResourceAttr(resourceName, "countries.#", "3"), - resource.TestCheckResourceAttr(resourceName, "currencies.#", "2"), - resource.TestCheckResourceAttr(resourceName, "languages.#", "4"), - resource.TestCheckResourceAttr(resourceName, "messages.0.enabled", "true"), - resource.TestCheckResourceAttr( - resourceName, "external_oauth.0.url", "https://example.com/oauth/token"), - resource.TestCheckResourceAttr( - resourceName, "external_oauth.0.authorization_header", "Bearer secret"), - resource.TestCheckResourceAttr( - resourceName, "shipping_rate_input_type", "CartValue"), - resource.TestCheckResourceAttr( - resourceName, "carts.0.country_tax_rate_fallback_enabled", "true"), + { + Config: testAccProjectConfigSetMyBusinessUnitStatusOnCreation("acctest_project_settings"), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(resourceName, "business_units.#", "1"), + resource.TestCheckResourceAttr( + resourceName, + "business_units.0.my_business_unit_status_on_creation", + string(platform.BusinessUnitStatusActive), + ), + ), + }, + { + Config: testAccProjectConfigSetMyBusinessUnitStatusOnCreation("acctest_project_settings"), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(resourceName, "business_units.#", "1"), + resource.TestCheckResourceAttr( + resourceName, + "business_units.0.my_business_unit_status_on_creation", + string(platform.BusinessUnitStatusActive), + ), + ), + }, + { + Config: testAccProjectConfigSetMyBusinessUnitAssociateRoleOnKeyCreation("acctest_project_settings"), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(resourceName, "business_units.#", "1"), + resource.TestCheckResourceAttr( + resourceName, + "business_units.0.my_business_unit_status_on_creation", + string(platform.BusinessUnitStatusInactive), + ), + resource.TestCheckResourceAttrSet( + resourceName, + "business_units.0.my_business_unit_associate_role_key_on_creation", ), - }, - */ + ), + }, }, }) } @@ -268,3 +284,68 @@ func testAccProjectConfigDeleteOAuthAndCarts(identifier string) string { "identifier": identifier, }) } + +func testAccProjectConfigSetMyBusinessUnitStatusOnCreation(identifier string) string { + return utils.HCLTemplate(` + resource "commercetools_project_settings" "{{ .identifier }}" { + name = "Test this thing new" + countries = ["NL", "DE", "US", "GB"] + currencies = ["EUR", "USD", "GBP"] + languages = ["nl", "de", "en", "en-US", "fr"] + messages { + enabled = false + } + + shipping_rate_input_type = "CartClassification" + shipping_rate_cart_classification_value { + key = "Small" + label = { + "en" = "Small" + "nl" = "Klein" + } + } + + business_units { + my_business_unit_status_on_creation = "Active" + } + }`, map[string]any{ + "identifier": identifier, + }) +} + +func testAccProjectConfigSetMyBusinessUnitAssociateRoleOnKeyCreation(identifier string) string { + return utils.HCLTemplate(` + resource commercetools_associate_role "my-role" { + key = "my-role" + name = "My role" + permissions = [ + "UpdateAssociates" + ] + } + + resource "commercetools_project_settings" "{{ .identifier }}" { + name = "Test this thing new" + countries = ["NL", "DE", "US", "GB"] + currencies = ["EUR", "USD", "GBP"] + languages = ["nl", "de", "en", "en-US", "fr"] + messages { + enabled = false + } + + shipping_rate_input_type = "CartClassification" + shipping_rate_cart_classification_value { + key = "Small" + label = { + "en" = "Small" + "nl" = "Klein" + } + } + + business_units { + my_business_unit_status_on_creation = "Inactive" + my_business_unit_associate_role_key_on_creation = commercetools_associate_role.my-role.key + } + }`, map[string]any{ + "identifier": identifier, + }) +}