Skip to content

Commit

Permalink
feat: fixed tests, added additional validations
Browse files Browse the repository at this point in the history
  • Loading branch information
demeyerthom committed Aug 16, 2024
1 parent a89928d commit f787489
Show file tree
Hide file tree
Showing 10 changed files with 292 additions and 158 deletions.
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ terraform-provider-commercetools_*
/dist/*
coverage.txt

// Terraform files
# Terraform files
.terraform/
terraform.*
crash.log


// Local files
# Local files
/local/*
!/local/*.example

// Go modules
# Go modules
vendor/

/.idea
/.env
8 changes: 3 additions & 5 deletions internal/resources/associate_role/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ func TestAssociateRoleResource_Create(t *testing.T) {
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(rn, "name", name),
resource.TestCheckResourceAttr(rn, "key", key),
resource.TestCheckResourceAttr(rn, "permissions.#", "7"),
resource.TestCheckResourceAttr(rn, "permissions.#", "6"),
),
},
{
Config: testAssociateRoleConfigUpdate(id, "Sales Manager - DACH", key, true, `"AddChildUnits"`),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(rn, "name", "Sales Manager - DACH"),
resource.TestCheckResourceAttr(rn, "key", key),
resource.TestCheckResourceAttr(rn, "permissions.#", "8"),
resource.TestCheckResourceAttr(rn, "permissions.7", "AddChildUnits"),
resource.TestCheckResourceAttr(rn, "permissions.#", "7"),
resource.TestCheckResourceAttr(rn, "permissions.6", "AddChildUnits"),
resource.TestCheckResourceAttr(rn, "buyer_assignable", "true"),
),
},
Expand All @@ -55,7 +55,6 @@ func testAssociateRoleConfig(identifier, name, key string) string {
buyer_assignable = false
name = "{{ .name }}"
permissions = [
"AddChildUnits",
"UpdateBusinessUnitDetails",
"UpdateAssociates",
"CreateMyCarts",
Expand All @@ -78,7 +77,6 @@ func testAssociateRoleConfigUpdate(identifier, name, key string, buyerAssign boo
buyer_assignable = {{ .buyer_assignable }}
name = "{{ .name }}"
permissions = [
"AddChildUnits",
"UpdateBusinessUnitDetails",
"UpdateAssociates",
"CreateMyCarts",
Expand Down
4 changes: 4 additions & 0 deletions internal/resources/business_unit_company/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ func (c *Company) updateActions(plan Company) (platform.BusinessUnitUpdate, erro
Actions: []platform.BusinessUnitUpdateAction{},
}

if !c.Key.Equal(plan.Key) {
return result, fmt.Errorf("key is immutable. Delete this resource instead if a change is intended")
}

if !c.Name.Equal(plan.Name) {
result.Actions = append(result.Actions, platform.BusinessUnitChangeNameAction{
Name: plan.Name.ValueString(),
Expand Down
17 changes: 9 additions & 8 deletions internal/resources/business_unit_company/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,21 @@ func NewCompanyResource() resource.Resource {
// Schema implements resource.Resource.
func (b *companyResource) Schema(_ context.Context, req resource.SchemaRequest, res *resource.SchemaResponse) {
res.Schema = schema.Schema{
MarkdownDescription: "Business Unit type to represent the top level of a business. Contains specific fields and values that differentiate a Company from the generic BusinessUnit.\n\n" +
MarkdownDescription: "Business Unit type to represent the top level of a business. Contains specific fields and values that differentiate a company from the generic business unit.\n\n" +
"See also the [Business Unit API Documentation](https://docs.commercetools.com/api/projects/business-units",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
MarkdownDescription: "Unique identifier of the Company.",
MarkdownDescription: "Unique identifier of the company.",
Computed: true,
},
"version": schema.Int64Attribute{
MarkdownDescription: "The current version of the Company.",
MarkdownDescription: "The current version of the company.",
Computed: true,
},
"key": schema.StringAttribute{
MarkdownDescription: "User-defined unique identifier for the Company.",
Required: true,
MarkdownDescription: "User-defined unique key for the company. Must be unique within the project. " +
"Updating this value is not supported.",
Required: true,
Validators: []validator.String{
stringvalidator.LengthBetween(2, 256),
stringvalidator.RegexMatches(
Expand All @@ -59,7 +60,7 @@ func (b *companyResource) Schema(_ context.Context, req resource.SchemaRequest,
},
},
"status": schema.StringAttribute{
MarkdownDescription: "The status of the Company.",
MarkdownDescription: "The status of the company.",
Optional: true,
Computed: true,
Default: stringdefault.StaticString(string(platform.BusinessUnitStatusActive)),
Expand All @@ -71,11 +72,11 @@ func (b *companyResource) Schema(_ context.Context, req resource.SchemaRequest,
},
},
"name": schema.StringAttribute{
MarkdownDescription: "The name of the Company.",
MarkdownDescription: "The name of the company.",
Required: true,
},
"contact_email": schema.StringAttribute{
MarkdownDescription: "The email address of the Company.",
MarkdownDescription: "The email address of the company.",
Optional: true,
},
"shipping_address_keys": schema.SetAttribute{
Expand Down
152 changes: 90 additions & 62 deletions internal/resources/business_unit_company/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package business_unit_company_test

import (
"context"
"github.com/labd/commercetools-go-sdk/platform"
"github.com/labd/terraform-provider-commercetools/internal/resources/business_unit_company"
"regexp"
"testing"

fwresource "github.com/hashicorp/terraform-plugin-framework/resource"
Expand Down Expand Up @@ -44,26 +46,35 @@ func TestBusinessUnitResource(t *testing.T) {
CheckDestroy: testBusinessUnitDestroy,
Steps: []resource.TestStep{
{
Config: businessUnitTFResourceDef("", "", ""),
Config: businessUnitTFResourceDef(),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(r, "key", "acme-company"),
resource.TestCheckResourceAttr(r, "name", "Acme Company Business Unit"),
resource.TestCheckResourceAttr(r, "status", "Active"),
resource.TestCheckResourceAttr(r, "stores.#", "2"),
resource.TestCheckResourceAttr(r, "stores.0.key", "acme-usa"),
resource.TestCheckResourceAttr(r, "stores.1.key", "acme-germany"),
resource.TestCheckResourceAttr(r, "addresses.#", "1"),
resource.TestCheckResourceAttr(r, "status", string(platform.BusinessUnitConfigurationStatusActive)),
resource.TestCheckResourceAttr(r, "contact_email", "acme@example.com"),
resource.TestCheckResourceAttr(r, "address.#", "1"),
),
},
{
Config: businessUnitTFResourceDef("Acme Business Unit - Updated", "Inactive", ""),
Config: businessUnitTFResourceDef(withBusinessUnitCompanyKey("acme-company-updated")),
ExpectError: regexp.MustCompile(`key is immutable`),
},
{
Config: businessUnitTFResourceDef(withBusinessUnitCompanyName("Acme Business Unit - Updated")),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(r, "key", "acme-company"),
resource.TestCheckResourceAttr(r, "status", "Inactive"),
resource.TestCheckResourceAttr(r, "stores.#", "2"),
resource.TestCheckResourceAttr(r, "stores.0.key", "acme-usa"),
resource.TestCheckResourceAttr(r, "stores.1.key", "acme-germany"),
resource.TestCheckResourceAttr(r, "addresses.#", "1"),
resource.TestCheckResourceAttr(r, "name", "Acme Business Unit - Updated"),
),
},
{
Config: businessUnitTFResourceDef(withBusinessUnitCompanyStatus(platform.BusinessUnitConfigurationStatusInactive)),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(r, "status", string(platform.BusinessUnitConfigurationStatusInactive)),
),
},
{
Config: businessUnitTFResourceDef(withBusinessUnitCompanyContactEmail("acme-updated@example.com")),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(r, "contact_email", "acme-updated@example.com"),
),
},
},
Expand All @@ -74,59 +85,76 @@ func testBusinessUnitDestroy(_ *terraform.State) error {
return nil
}

func businessUnitTFResourceDef(name, status, email string) string {
if status == "" {
status = "Active"
type option func(map[string]interface{})

func withBusinessUnitCompanyKey(key string) option {
return func(data map[string]interface{}) {
data["key"] = key
}
}

func withBusinessUnitCompanyName(name string) option {
return func(data map[string]interface{}) {
data["name"] = name
}
}

func withBusinessUnitCompanyStatus(status platform.BusinessUnitConfigurationStatus) option {
return func(data map[string]interface{}) {
data["status"] = status
}
}

if email == "" {
email = "acme@example.com"
func withBusinessUnitCompanyContactEmail(email string) option {
return func(data map[string]interface{}) {
data["contact_email"] = email
}
}

if name == "" {
name = "Acme Company Business Unit"
func businessUnitTFResourceDef(options ...option) string {
data := map[string]interface{}{
"key": "acme-company",
"status": platform.BusinessUnitConfigurationStatusActive,
"contact_email": "acme@example.com",
"name": "Acme Company Business Unit",
}

return utils.HCLTemplate(`resource "commercetools_business_unit_company" "acme_company" {
key = "acme-company"
name = {{ .name }}
status = {{ .status }}
contact_email = {{ .email}}
store {
key = "acme-usa"
}
store {
key = "acme-germany"
}
address {
key = "acme-business-unit-address"
title = "Acme Business Unit Address"
salutation = "Mr."
first_name = "John"
last_name = "Doe"
street_name = "Main Street"
street_number = "1"
additional_street_info = "Additional Street Info"
postal_code = "12345"
city = "Berlin"
region = "Berlin"
country = "DE"
company = "Acme"
department = "IT"
building = "Building"
apartment = "Apartment"
po_box = "P.O. Box"
phone = "123456789"
mobile = "987654321"
}
default_shipping_address_id = "acme-business-unit-address"
default_billing_address_id = "acme-business-unit-address"
}`, map[string]any{
"name": name,
"status": status,
"email": email,
})
for _, option := range options {
option(data)
}

return utils.HCLTemplate(`
resource "commercetools_business_unit_company" "acme_company" {
key = "{{ .key }}"
name = "{{ .name }}"
status = "{{ .status }}"
contact_email = "{{ .contact_email }}"
address {
key = "acme-business-unit-address"
title = "Acme Business Unit Address"
salutation = "Mr."
first_name = "John"
last_name = "Doe"
street_name = "Main Street"
street_number = "1"
additional_street_info = "Additional Street Info"
postal_code = "12345"
city = "Berlin"
region = "Berlin"
country = "DE"
company = "Acme"
department = "IT"
building = "Building"
apartment = "Apartment"
po_box = "P.O. Box"
phone = "123456789"
mobile = "987654321"
}
shipping_address_keys = ["acme-business-unit-address"]
billing_address_keys = ["acme-business-unit-address"]
default_shipping_address_key = "acme-business-unit-address"
default_billing_address_key = "acme-business-unit-address"
}
`, data)
}
4 changes: 4 additions & 0 deletions internal/resources/business_unit_division/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ func (d *Division) updateActions(plan Division) (platform.BusinessUnitUpdate, er
Actions: []platform.BusinessUnitUpdateAction{},
}

if !d.Key.Equal(plan.Key) {
return result, fmt.Errorf("key is immutable. Delete this resource instead if a change is intended")
}

if !d.Name.Equal(plan.Name) {
result.Actions = append(result.Actions, platform.BusinessUnitChangeNameAction{
Name: plan.Name.ValueString(),
Expand Down
Loading

0 comments on commit f787489

Please sign in to comment.