Skip to content

Commit

Permalink
Merge pull request #530 from labd/529-add-support-for-project-product…
Browse files Browse the repository at this point in the history
…searchindexingmode

feat: added handling of product search and customer search indexes
  • Loading branch information
demeyerthom authored Nov 1, 2024
2 parents 0133db9 + fbc3fa5 commit e8182a8
Show file tree
Hide file tree
Showing 9 changed files with 273 additions and 34 deletions.
3 changes: 3 additions & 0 deletions .changes/unreleased/Added-20241022-103914.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Added
body: Added handling of product search and customer search indexes
time: 2024-10-22T10:39:14.745921232+02:00
4 changes: 3 additions & 1 deletion docs/resources/project_settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ resource "commercetools_project_settings" "my-project" {
- `carts` (Block List) [Carts Configuration](https://docs.commercetools.com/api/projects/project#carts-configuration) (see [below for nested schema](#nestedblock--carts))
- `countries` (List of String) A two-digit country code as per [ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2)
- `currencies` (List of String) A three-digit currency code as per [ISO 4217](https://en.wikipedia.org/wiki/ISO_4217)
- `enable_search_index_customers` (Boolean) Enable the Search Indexing of customers
- `enable_search_index_orders` (Boolean) Enable the Search Indexing of orders
- `enable_search_index_products` (Boolean) Enable the Search Indexing of products
- `enable_search_index_product_search` (Boolean) Enable the Search Indexing of products
- `enable_search_index_products` (Boolean) Enable the Search Indexing of product projections
- `external_oauth` (Block List) [External OAUTH](https://docs.commercetools.com/api/projects/project#externaloauth) (see [below for nested schema](#nestedblock--external_oauth))
- `languages` (List of String) [IETF Language Tag](https://en.wikipedia.org/wiki/IETF_language_tag)
- `messages` (Block List) The change notifications subscribed to (see [below for nested schema](#nestedblock--messages))
Expand Down
54 changes: 48 additions & 6 deletions internal/resources/project/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ type Project struct {
Countries []types.String `tfsdk:"countries"`
Languages []types.String `tfsdk:"languages"`

EnableSearchIndexProducts types.Bool `tfsdk:"enable_search_index_products"`
EnableSearchIndexOrders types.Bool `tfsdk:"enable_search_index_orders"`
EnableSearchIndexProducts types.Bool `tfsdk:"enable_search_index_products"`
EnableSearchIndexProductSearch types.Bool `tfsdk:"enable_search_index_product_search"`
EnableSearchIndexOrders types.Bool `tfsdk:"enable_search_index_orders"`
EnableSearchIndexCustomers types.Bool `tfsdk:"enable_search_index_customers"`

// These items all have maximal one item. We don't use SingleNestedBlock
// here since it isn't quite robust currently.
Expand All @@ -53,8 +55,10 @@ func NewProjectFromNative(n *platform.Project) Project {
Countries: pie.Map(n.Countries, types.StringValue),
Languages: pie.Map(n.Languages, types.StringValue),

EnableSearchIndexProducts: types.BoolValue(false),
EnableSearchIndexOrders: types.BoolValue(false),
EnableSearchIndexProducts: types.BoolValue(false),
EnableSearchIndexProductSearch: types.BoolValue(false),
EnableSearchIndexOrders: types.BoolValue(false),
EnableSearchIndexCustomers: types.BoolValue(false),

Carts: []Carts{
{
Expand Down Expand Up @@ -101,12 +105,24 @@ func NewProjectFromNative(n *platform.Project) Project {
res.EnableSearchIndexProducts = types.BoolValue(enabled)
}

if n.SearchIndexing != nil && n.SearchIndexing.ProductsSearch != nil && n.SearchIndexing.ProductsSearch.Status != nil {
status := *n.SearchIndexing.ProductsSearch.Status
enabled := status != platform.SearchIndexingConfigurationStatusDeactivated
res.EnableSearchIndexProductSearch = types.BoolValue(enabled)
}

if n.SearchIndexing != nil && n.SearchIndexing.Orders != nil && n.SearchIndexing.Orders.Status != nil {
status := *n.SearchIndexing.Orders.Status
enabled := status != platform.SearchIndexingConfigurationStatusDeactivated
res.EnableSearchIndexOrders = types.BoolValue(enabled)
}

if n.SearchIndexing != nil && n.SearchIndexing.Customers != nil && n.SearchIndexing.Customers.Status != nil {
status := *n.SearchIndexing.Customers.Status
enabled := status != platform.SearchIndexingConfigurationStatusDeactivated
res.EnableSearchIndexCustomers = types.BoolValue(enabled)
}

if n.ExternalOAuth != nil {
res.ExternalOAuth = []ExternalOAuth{
{
Expand Down Expand Up @@ -266,11 +282,37 @@ func (p *Project) updateActions(plan Project) (platform.ProjectUpdate, error) {
)
}

// changeProductSearchIndexingEnabled
if !(p.EnableSearchIndexProducts.ValueBool() == plan.EnableSearchIndexProducts.ValueBool()) {
// changeProductSearchIndexingEnabled (ProductProjectionsSearch)
if !p.EnableSearchIndexProducts.Equal(plan.EnableSearchIndexProducts) {
var mode = platform.ProductSearchIndexingModeProductProjectionsSearch
result.Actions = append(result.Actions,
platform.ProjectChangeProductSearchIndexingEnabledAction{
Enabled: plan.EnableSearchIndexProducts.ValueBool(),
Mode: &mode,
},
)
}

// changeProductSearchIndexingEnabled (ProductsSearch)
if !p.EnableSearchIndexProductSearch.Equal(plan.EnableSearchIndexProductSearch) {
var mode = platform.ProductSearchIndexingModeProductsSearch
result.Actions = append(result.Actions,
platform.ProjectChangeProductSearchIndexingEnabledAction{
Enabled: plan.EnableSearchIndexProductSearch.ValueBool(),
Mode: &mode,
},
)
}

// changeCustomerSearchStatus
if !p.EnableSearchIndexCustomers.Equal(plan.EnableSearchIndexCustomers) {
status := platform.CustomerSearchStatusDeactivated
if plan.EnableSearchIndexCustomers.ValueBool() {
status = platform.CustomerSearchStatusActivated
}
result.Actions = append(result.Actions,
platform.ProjectChangeCustomerSearchStatusAction{
Status: status,
},
)
}
Expand Down
177 changes: 169 additions & 8 deletions internal/resources/project/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ func TestNewProjectFromNative(t *testing.T) {
Key: types.StringValue("my-project"),
Name: types.StringValue("my project"),

EnableSearchIndexProducts: types.BoolValue(false),
EnableSearchIndexOrders: types.BoolValue(false),
EnableSearchIndexProducts: types.BoolValue(false),
EnableSearchIndexOrders: types.BoolValue(false),
EnableSearchIndexCustomers: types.BoolValue(false),
EnableSearchIndexProductSearch: types.BoolValue(false),

ExternalOAuth: []ExternalOAuth{},
Carts: []Carts{
Expand Down Expand Up @@ -157,17 +159,15 @@ func TestUpdateActions(t *testing.T) {
},
},
{
name: "Create with bool unknown",
name: "Update with search index orders activated",
state: Project{
Version: types.Int64Value(1),
EnableSearchIndexOrders: types.BoolValue(false),
EnableSearchIndexProducts: types.BoolValue(false),
Version: types.Int64Value(1),
EnableSearchIndexOrders: types.BoolValue(false),
},
plan: Project{
Version: types.Int64Value(1),

EnableSearchIndexOrders: types.BoolValue(true),
EnableSearchIndexProducts: types.BoolUnknown(),
EnableSearchIndexOrders: types.BoolValue(true),
},
action: platform.ProjectUpdate{
Version: 1,
Expand All @@ -176,6 +176,167 @@ func TestUpdateActions(t *testing.T) {
},
},
},
{
name: "Update with search index orders deactivated",
state: Project{
Version: types.Int64Value(1),
EnableSearchIndexOrders: types.BoolValue(true),
},
plan: Project{
Version: types.Int64Value(1),
EnableSearchIndexOrders: types.BoolValue(false),
},
action: platform.ProjectUpdate{
Version: 1,
Actions: []platform.ProjectUpdateAction{
platform.ProjectChangeOrderSearchStatusAction{Status: platform.OrderSearchStatusDeactivated},
},
},
},
{
name: "Update with search index orders no changes",
state: Project{
Version: types.Int64Value(1),
EnableSearchIndexOrders: types.BoolValue(false),
},
plan: Project{
Version: types.Int64Value(1),
EnableSearchIndexOrders: types.BoolValue(false),
},
action: platform.ProjectUpdate{
Version: 1,
Actions: []platform.ProjectUpdateAction{},
},
},
{
name: "Update with search index customers activated",
state: Project{
Version: types.Int64Value(1),
EnableSearchIndexCustomers: types.BoolValue(false),
},
plan: Project{
Version: types.Int64Value(1),
EnableSearchIndexCustomers: types.BoolValue(true),
},
action: platform.ProjectUpdate{
Version: 1,
Actions: []platform.ProjectUpdateAction{
platform.ProjectChangeCustomerSearchStatusAction{Status: platform.CustomerSearchStatusActivated},
},
},
},
{
name: "Update with search index customers deactivated",
state: Project{
Version: types.Int64Value(1),
EnableSearchIndexCustomers: types.BoolValue(true),
},
plan: Project{
Version: types.Int64Value(1),
EnableSearchIndexCustomers: types.BoolValue(false),
},
action: platform.ProjectUpdate{
Version: 1,
Actions: []platform.ProjectUpdateAction{
platform.ProjectChangeCustomerSearchStatusAction{Status: platform.CustomerSearchStatusDeactivated},
},
},
},
{
name: "Update with search index customers no changes",
state: Project{
Version: types.Int64Value(1),
EnableSearchIndexCustomers: types.BoolValue(false),
},
plan: Project{
Version: types.Int64Value(1),
EnableSearchIndexCustomers: types.BoolValue(false),
},
action: platform.ProjectUpdate{
Version: 1,
Actions: []platform.ProjectUpdateAction{},
},
},
{
name: "Update with search index products activated",
state: Project{
Version: types.Int64Value(1),
EnableSearchIndexProducts: types.BoolValue(false),
},
plan: Project{
Version: types.Int64Value(1),
EnableSearchIndexProducts: types.BoolValue(true),
},
action: platform.ProjectUpdate{
Version: 1,
Actions: []platform.ProjectUpdateAction{
platform.ProjectChangeProductSearchIndexingEnabledAction{
Enabled: true,
Mode: utils.GetRef(platform.ProductSearchIndexingModeProductProjectionsSearch),
},
},
},
},
{
name: "Update with search index products deactivated",
state: Project{
Version: types.Int64Value(1),
EnableSearchIndexProducts: types.BoolValue(true),
},
plan: Project{
Version: types.Int64Value(1),
EnableSearchIndexProducts: types.BoolValue(false),
},
action: platform.ProjectUpdate{
Version: 1,
Actions: []platform.ProjectUpdateAction{
platform.ProjectChangeProductSearchIndexingEnabledAction{
Enabled: false,
Mode: utils.GetRef(platform.ProductSearchIndexingModeProductProjectionsSearch),
},
},
},
},
{
name: "Update with search index product search activated",
state: Project{
Version: types.Int64Value(1),
EnableSearchIndexProductSearch: types.BoolValue(false),
},
plan: Project{
Version: types.Int64Value(1),
EnableSearchIndexProductSearch: types.BoolValue(true),
},
action: platform.ProjectUpdate{
Version: 1,
Actions: []platform.ProjectUpdateAction{
platform.ProjectChangeProductSearchIndexingEnabledAction{
Enabled: true,
Mode: utils.GetRef(platform.ProductSearchIndexingModeProductsSearch),
},
},
},
},
{
name: "Update with search index product search deactivated",
state: Project{
Version: types.Int64Value(1),
EnableSearchIndexProductSearch: types.BoolValue(true),
},
plan: Project{
Version: types.Int64Value(1),
EnableSearchIndexProductSearch: types.BoolValue(false),
},
action: platform.ProjectUpdate{
Version: 1,
Actions: []platform.ProjectUpdateAction{
platform.ProjectChangeProductSearchIndexingEnabledAction{
Enabled: false,
Mode: utils.GetRef(platform.ProductSearchIndexingModeProductsSearch),
},
},
},
},
{
name: "Create with business unit settings",
state: Project{
Expand Down
27 changes: 18 additions & 9 deletions internal/resources/project/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package project

import (
"context"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
"time"

Expand All @@ -18,7 +19,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
sdk_resource "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
sdkresource "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/labd/commercetools-go-sdk/platform"

"github.com/labd/terraform-provider-commercetools/internal/customtypes"
Expand Down Expand Up @@ -114,20 +115,28 @@ func (r *projectResource) Schema(_ context.Context, _ resource.SchemaRequest, re
},
},
"enable_search_index_products": schema.BoolAttribute{
MarkdownDescription: "Enable the Search Indexing of product projections",
Optional: true,
Computed: true,
Default: booldefault.StaticBool(false),
},
"enable_search_index_product_search": schema.BoolAttribute{
MarkdownDescription: "Enable the Search Indexing of products",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Bool{
boolplanmodifier.UseStateForUnknown(),
},
Default: booldefault.StaticBool(false),
},
"enable_search_index_orders": schema.BoolAttribute{
MarkdownDescription: "Enable the Search Indexing of orders",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Bool{
boolplanmodifier.UseStateForUnknown(),
},
Default: booldefault.StaticBool(false),
},
"enable_search_index_customers": schema.BoolAttribute{
MarkdownDescription: "Enable the Search Indexing of customers",
Optional: true,
Computed: true,
Default: booldefault.StaticBool(false),
},
"shipping_rate_input_type": schema.StringAttribute{
MarkdownDescription: "Three ways to dynamically select a ShippingRatePriceTier exist. The CartValue type uses " +
Expand Down Expand Up @@ -327,7 +336,7 @@ func (r *projectResource) Create(ctx context.Context, req resource.CreateRequest
}

var res *platform.Project
err = sdk_resource.RetryContext(ctx, 5*time.Second, func() *sdk_resource.RetryError {
err = sdkresource.RetryContext(ctx, 5*time.Second, func() *sdkresource.RetryError {
var err error
res, err = r.client.Post(input).Execute(ctx)
return utils.ProcessRemoteError(err)
Expand Down Expand Up @@ -402,7 +411,7 @@ func (r *projectResource) Update(ctx context.Context, req resource.UpdateRequest
}

var res *platform.Project
err = sdk_resource.RetryContext(ctx, 5*time.Second, func() *sdk_resource.RetryError {
err = sdkresource.RetryContext(ctx, 5*time.Second, func() *sdkresource.RetryError {
var err error
res, err = r.client.Post(input).Execute(ctx)
return utils.ProcessRemoteError(err)
Expand Down
Loading

0 comments on commit e8182a8

Please sign in to comment.