Skip to content

Commit

Permalink
feat(business-unit): add support for divisions and companies as separ…
Browse files Browse the repository at this point in the history
…ate resources
  • Loading branch information
VictorAvelar committed May 24, 2024
1 parent eaf04b1 commit 4acc70d
Show file tree
Hide file tree
Showing 6 changed files with 1,251 additions and 29 deletions.
57 changes: 57 additions & 0 deletions examples/resources/commercetools_business_unit/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
resource "commercetools_business_unit_company" "acme_company" {
key = "acme-company"
name = "The ACME Company"
status = "Active"
contact_email = "[email protected]"

store {
key = "acme-usa"
type_id = "store"
}

store {
key = "acme-germany"
type_id = "store"
}

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"
}

resource "commercetools_business_unit_division" "acme-willie-coyote" {
key = "acme-willie-coyote"
name = "Willie Coyote - Traps for Roadrunners"
status = "Active"
contact_email = "[email protected]"
store_mode = "FromParent"
associate_mode = "ExplicitAndFromParent"

// Only available for division business units as the Company
// business unit has no parent unit and must always be the Top Level Unit.
parent_unit {
key = commercetools_business_unit_company.acme-company.key
type_id = "company"
}
}
3 changes: 3 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
datasourcetype "github.com/labd/terraform-provider-commercetools/internal/datasource/type"
"github.com/labd/terraform-provider-commercetools/internal/resources/associate_role"
"github.com/labd/terraform-provider-commercetools/internal/resources/attribute_group"
"github.com/labd/terraform-provider-commercetools/internal/resources/business_unit"
"github.com/labd/terraform-provider-commercetools/internal/resources/product_selection"
"github.com/labd/terraform-provider-commercetools/internal/resources/project"
"github.com/labd/terraform-provider-commercetools/internal/resources/state"
Expand Down Expand Up @@ -196,5 +197,7 @@ func (p *ctProvider) Resources(_ context.Context) []func() resource.Resource {
attribute_group.NewResource,
associate_role.NewResource,
product_selection.NewResource,
business_unit.NewCompanyResource,
business_unit.NewDivisionResource,
}
}
117 changes: 104 additions & 13 deletions internal/resources/business_unit/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/elliotchance/pie/v2"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/labd/commercetools-go-sdk/platform"
"github.com/labd/terraform-provider-commercetools/internal/utils"
)

const (
Expand Down Expand Up @@ -43,22 +44,20 @@ type Company struct {
ID types.String `tfsdk:"id"`
Version types.Int64 `tfsdk:"version"`
Key types.String `tfsdk:"key"`
Status types.String `tfsdk:"status"`
Stores []StoreKeyReference `tfsdk:"store"`
Name types.String `tfsdk:"name"`
Status types.String `tfsdk:"status"`
ContactEmail types.String `tfsdk:"contact_email"`
Stores []StoreKeyReference `tfsdk:"store"`
Addresses []Address `tfsdk:"address"`
ShippingAddressIDs []types.String `tfsdk:"shipping_address_ids"`
DefaultShippingAddressID types.String `tfsdk:"default_shipping_address_id"`
BillingAddressIDs []types.String `tfsdk:"billing_address_ids"`
DefaultBillingAddressID types.String `tfsdk:"default_billing_address_id"`
AssociateMode types.String `tfsdk:"associate_mode"`
Associates []Associate `tfsdk:"associate"`
}

func (c Company) draft() platform.CompanyDraft {
status := platform.BusinessUnitStatus(c.Status.ValueString())
associateMode := platform.BusinessUnitAssociateMode(c.AssociateMode.ValueString())
dsa := pie.Int(c.DefaultShippingAddressID.ValueString())
dba := pie.Int(c.DefaultBillingAddressID.ValueString())

Expand All @@ -71,9 +70,8 @@ func (c Company) draft() platform.CompanyDraft {
ID: s.Key.ValueStringPointer(),
}
}),
Name: c.Name.ValueString(),
ContactEmail: c.ContactEmail.ValueStringPointer(),
AssociateMode: &associateMode,
Name: c.Name.ValueString(),
ContactEmail: c.ContactEmail.ValueStringPointer(),
Associates: pie.Map(c.Associates, func(a Associate) platform.AssociateDraft {
return a.draft()
}),
Expand Down Expand Up @@ -127,12 +125,6 @@ func (c Company) updateActions(plan Company) platform.BusinessUnitUpdate {
})
}

if c.AssociateMode.ValueString() != plan.AssociateMode.ValueString() {
result.Actions = append(result.Actions, platform.BusinessUnitChangeAssociateModeAction{
AssociateMode: platform.BusinessUnitAssociateMode(plan.AssociateMode.ValueString()),
})
}

if !reflect.DeepEqual(c.Stores, plan.Stores) {
// find stores to be added
for _, store := range plan.Stores {
Expand Down Expand Up @@ -229,6 +221,50 @@ func (c Company) updateActions(plan Company) platform.BusinessUnitUpdate {
return result
}

// NewCompanyFromNative creates a new Company from a platform.Company.
func NewCompanyFromNative(cc platform.BusinessUnit) Company {
c, ok := cc.(Company)
if !ok {
return Company{}
}

company := Company{
ID: types.StringPointerValue(utils.StringRef(c.ID)),
Version: types.Int64PointerValue(c.Version.ValueInt64Pointer()),
Key: types.StringPointerValue(utils.StringRef(c.Key)),
Name: types.StringPointerValue(utils.StringRef(c.Name)),
Status: types.StringPointerValue(utils.StringRef(c.Status)),
ContactEmail: types.StringPointerValue(c.ContactEmail.ValueStringPointer()),
DefaultShippingAddressID: types.StringPointerValue(c.DefaultShippingAddressID.ValueStringPointer()),
DefaultBillingAddressID: types.StringPointerValue(c.DefaultBillingAddressID.ValueStringPointer()),
Stores: make([]StoreKeyReference, len(c.Stores)),
Addresses: make([]Address, len(c.Addresses)),
ShippingAddressIDs: make([]types.String, len(c.ShippingAddressIDs)),
BillingAddressIDs: make([]types.String, len(c.BillingAddressIDs)),
Associates: make([]Associate, len(c.Associates)),
}

for i, store := range c.Stores {
company.Stores[i] = StoreKeyReference{
Key: types.StringValue(store.Key.ValueString()),
}
}

copy(company.Addresses, c.Addresses)

for i, id := range c.ShippingAddressIDs {
company.ShippingAddressIDs[i] = types.StringValue(id.ValueString())
}

for i, id := range c.BillingAddressIDs {
company.BillingAddressIDs[i] = types.StringValue(id.ValueString())
}

copy(company.Associates, c.Associates)

return company
}

type Division struct {
ID types.String `tfsdk:"id"`
Version types.Int64 `tfsdk:"version"`
Expand Down Expand Up @@ -434,6 +470,61 @@ func (d Division) updateActions(plan Division) platform.BusinessUnitUpdate {
return result
}

// NewDivisionFromNative creates a new Division from a platform.Company.
func NewDivisionFromNative(cc platform.BusinessUnit) Division {
c, ok := cc.(Division)
if !ok {
return Division{}
}

parent := BusinessUnitResourceIdentifier{
ID: types.StringValue(c.ParentUnit.ID.ValueString()),
Key: types.StringValue(c.ParentUnit.Key.ValueString()),
}

company := Division{
ID: types.StringPointerValue(utils.StringRef(c.ID)),
Version: types.Int64PointerValue(c.Version.ValueInt64Pointer()),
Key: types.StringPointerValue(utils.StringRef(c.Key)),
Status: types.StringPointerValue(utils.StringRef(c.Status)),
ParentUnit: parent,
StoreMode: types.StringValue(c.StoreMode.ValueString()),
Name: types.StringPointerValue(utils.StringRef(c.Name)),
ContactEmail: types.StringPointerValue(c.ContactEmail.ValueStringPointer()),
DefaultShippingAddressID: types.StringPointerValue(c.DefaultShippingAddressID.ValueStringPointer()),
DefaultBillingAddressID: types.StringPointerValue(c.DefaultBillingAddressID.ValueStringPointer()),
AssociateMode: types.StringPointerValue(utils.StringRef(c.AssociateMode)),
Stores: make([]StoreKeyReference, len(c.Stores)),
BillingAddressIDs: make([]types.String, len(c.BillingAddressIDs)),
Addresses: make([]Address, len(c.Addresses)),
ShippingAddressIDs: make([]types.String, len(c.ShippingAddressIDs)),
Associates: make([]Associate, len(c.Associates)),
InheritedAssociates: make([]InheritedAssociate, len(c.InheritedAssociates)),
}

for i, store := range c.Stores {
company.Stores[i] = StoreKeyReference{
Key: types.StringValue(store.Key.ValueString()),
}
}

copy(company.Addresses, c.Addresses)

for i, id := range c.ShippingAddressIDs {
company.ShippingAddressIDs[i] = types.StringValue(id.ValueString())
}

for i, id := range c.BillingAddressIDs {
company.BillingAddressIDs[i] = types.StringValue(id.ValueString())
}

copy(company.Associates, c.Associates)

copy(company.InheritedAssociates, c.InheritedAssociates)

return company
}

/*
Support types for the business unit resource.
*/
Expand Down
16 changes: 0 additions & 16 deletions internal/resources/business_unit/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,22 +96,6 @@ func TestBusinessUnit_Company_UpdateActions(t *testing.T) {
},
},
},
{
"business unit update associate mode",
Company{
AssociateMode: types.StringValue("Explicit"),
},
Company{
AssociateMode: types.StringValue("ExplicitAndFromParent"),
},
platform.BusinessUnitUpdate{
Actions: []platform.BusinessUnitUpdateAction{
platform.BusinessUnitChangeAssociateModeAction{
AssociateMode: "ExplicitAndFromParent",
},
},
},
},
{
"business unit update stores",
Company{
Expand Down
Loading

0 comments on commit 4acc70d

Please sign in to comment.