Skip to content

Commit 633ca12

Browse files
committed
feat: added handling of product search and customer search indexes
1 parent 602d153 commit 633ca12

File tree

8 files changed

+258
-28
lines changed

8 files changed

+258
-28
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
kind: Added
2+
body: Added handling of product search and customer search indexes
3+
time: 2024-10-22T10:39:14.745921232+02:00

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ vendor/
1919

2020
/.idea
2121
/.env
22+
/go.work*

go.mod

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ module github.com/labd/terraform-provider-commercetools
22

33
go 1.21
44

5-
//replace github.com/labd/commercetools-go-sdk v1.5.1 => ../commercetools-go-sdk
6-
75
require (
86
github.com/elliotchance/orderedmap/v2 v2.4.0
97
github.com/elliotchance/pie/v2 v2.9.0
@@ -14,7 +12,7 @@ require (
1412
github.com/hashicorp/terraform-plugin-go v0.23.0
1513
github.com/hashicorp/terraform-plugin-mux v0.16.0
1614
github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0
17-
github.com/labd/commercetools-go-sdk v1.6.0
15+
github.com/labd/commercetools-go-sdk v1.7.0
1816
github.com/mitchellh/mapstructure v1.5.0
1917
github.com/stretchr/testify v1.9.0
2018
golang.org/x/oauth2 v0.22.0

internal/resources/project/model.go

+48-6
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ type Project struct {
2626
Countries []types.String `tfsdk:"countries"`
2727
Languages []types.String `tfsdk:"languages"`
2828

29-
EnableSearchIndexProducts types.Bool `tfsdk:"enable_search_index_products"`
30-
EnableSearchIndexOrders types.Bool `tfsdk:"enable_search_index_orders"`
29+
EnableSearchIndexProducts types.Bool `tfsdk:"enable_search_index_products"`
30+
EnableSearchIndexProductSearch types.Bool `tfsdk:"enable_search_index_product_search"`
31+
EnableSearchIndexOrders types.Bool `tfsdk:"enable_search_index_orders"`
32+
EnableSearchIndexCustomers types.Bool `tfsdk:"enable_search_index_customers"`
3133

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

56-
EnableSearchIndexProducts: types.BoolValue(false),
57-
EnableSearchIndexOrders: types.BoolValue(false),
58+
EnableSearchIndexProducts: types.BoolValue(false),
59+
EnableSearchIndexProductSearch: types.BoolValue(false),
60+
EnableSearchIndexOrders: types.BoolValue(false),
61+
EnableSearchIndexCustomers: types.BoolValue(false),
5862

5963
Carts: []Carts{
6064
{
@@ -101,12 +105,24 @@ func NewProjectFromNative(n *platform.Project) Project {
101105
res.EnableSearchIndexProducts = types.BoolValue(enabled)
102106
}
103107

108+
if n.SearchIndexing != nil && n.SearchIndexing.ProductsSearch != nil && n.SearchIndexing.ProductsSearch.Status != nil {
109+
status := *n.SearchIndexing.ProductsSearch.Status
110+
enabled := status != platform.SearchIndexingConfigurationStatusDeactivated
111+
res.EnableSearchIndexProductSearch = types.BoolValue(enabled)
112+
}
113+
104114
if n.SearchIndexing != nil && n.SearchIndexing.Orders != nil && n.SearchIndexing.Orders.Status != nil {
105115
status := *n.SearchIndexing.Orders.Status
106116
enabled := status != platform.SearchIndexingConfigurationStatusDeactivated
107117
res.EnableSearchIndexOrders = types.BoolValue(enabled)
108118
}
109119

120+
if n.SearchIndexing != nil && n.SearchIndexing.Customers != nil && n.SearchIndexing.Customers.Status != nil {
121+
status := *n.SearchIndexing.Customers.Status
122+
enabled := status != platform.SearchIndexingConfigurationStatusDeactivated
123+
res.EnableSearchIndexCustomers = types.BoolValue(enabled)
124+
}
125+
110126
if n.ExternalOAuth != nil {
111127
res.ExternalOAuth = []ExternalOAuth{
112128
{
@@ -266,11 +282,37 @@ func (p *Project) updateActions(plan Project) (platform.ProjectUpdate, error) {
266282
)
267283
}
268284

269-
// changeProductSearchIndexingEnabled
270-
if !(p.EnableSearchIndexProducts.ValueBool() == plan.EnableSearchIndexProducts.ValueBool()) {
285+
// changeProductSearchIndexingEnabled (ProductProjectionsSearch)
286+
if !p.EnableSearchIndexProducts.Equal(plan.EnableSearchIndexProducts) {
287+
var mode = platform.ProductSearchIndexingModeProductProjectionsSearch
271288
result.Actions = append(result.Actions,
272289
platform.ProjectChangeProductSearchIndexingEnabledAction{
273290
Enabled: plan.EnableSearchIndexProducts.ValueBool(),
291+
Mode: &mode,
292+
},
293+
)
294+
}
295+
296+
// changeProductSearchIndexingEnabled (ProductsSearch)
297+
if !p.EnableSearchIndexProductSearch.Equal(plan.EnableSearchIndexProductSearch) {
298+
var mode = platform.ProductSearchIndexingModeProductsSearch
299+
result.Actions = append(result.Actions,
300+
platform.ProjectChangeProductSearchIndexingEnabledAction{
301+
Enabled: plan.EnableSearchIndexProductSearch.ValueBool(),
302+
Mode: &mode,
303+
},
304+
)
305+
}
306+
307+
// changeCustomerSearchStatus
308+
if !p.EnableSearchIndexCustomers.Equal(plan.EnableSearchIndexCustomers) {
309+
status := platform.CustomerSearchStatusDeactivated
310+
if plan.EnableSearchIndexCustomers.ValueBool() {
311+
status = platform.CustomerSearchStatusActivated
312+
}
313+
result.Actions = append(result.Actions,
314+
platform.ProjectChangeCustomerSearchStatusAction{
315+
Status: status,
274316
},
275317
)
276318
}

internal/resources/project/model_test.go

+169-8
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ func TestNewProjectFromNative(t *testing.T) {
3232
Key: types.StringValue("my-project"),
3333
Name: types.StringValue("my project"),
3434

35-
EnableSearchIndexProducts: types.BoolValue(false),
36-
EnableSearchIndexOrders: types.BoolValue(false),
35+
EnableSearchIndexProducts: types.BoolValue(false),
36+
EnableSearchIndexOrders: types.BoolValue(false),
37+
EnableSearchIndexCustomers: types.BoolValue(false),
38+
EnableSearchIndexProductSearch: types.BoolValue(false),
3739

3840
ExternalOAuth: []ExternalOAuth{},
3941
Carts: []Carts{
@@ -157,17 +159,15 @@ func TestUpdateActions(t *testing.T) {
157159
},
158160
},
159161
{
160-
name: "Create with bool unknown",
162+
name: "Update with search index orders activated",
161163
state: Project{
162-
Version: types.Int64Value(1),
163-
EnableSearchIndexOrders: types.BoolValue(false),
164-
EnableSearchIndexProducts: types.BoolValue(false),
164+
Version: types.Int64Value(1),
165+
EnableSearchIndexOrders: types.BoolValue(false),
165166
},
166167
plan: Project{
167168
Version: types.Int64Value(1),
168169

169-
EnableSearchIndexOrders: types.BoolValue(true),
170-
EnableSearchIndexProducts: types.BoolUnknown(),
170+
EnableSearchIndexOrders: types.BoolValue(true),
171171
},
172172
action: platform.ProjectUpdate{
173173
Version: 1,
@@ -176,6 +176,167 @@ func TestUpdateActions(t *testing.T) {
176176
},
177177
},
178178
},
179+
{
180+
name: "Update with search index orders deactivated",
181+
state: Project{
182+
Version: types.Int64Value(1),
183+
EnableSearchIndexOrders: types.BoolValue(true),
184+
},
185+
plan: Project{
186+
Version: types.Int64Value(1),
187+
EnableSearchIndexOrders: types.BoolValue(false),
188+
},
189+
action: platform.ProjectUpdate{
190+
Version: 1,
191+
Actions: []platform.ProjectUpdateAction{
192+
platform.ProjectChangeOrderSearchStatusAction{Status: platform.OrderSearchStatusDeactivated},
193+
},
194+
},
195+
},
196+
{
197+
name: "Update with search index orders no changes",
198+
state: Project{
199+
Version: types.Int64Value(1),
200+
EnableSearchIndexOrders: types.BoolValue(false),
201+
},
202+
plan: Project{
203+
Version: types.Int64Value(1),
204+
EnableSearchIndexOrders: types.BoolValue(false),
205+
},
206+
action: platform.ProjectUpdate{
207+
Version: 1,
208+
Actions: []platform.ProjectUpdateAction{},
209+
},
210+
},
211+
{
212+
name: "Update with search index customers activated",
213+
state: Project{
214+
Version: types.Int64Value(1),
215+
EnableSearchIndexCustomers: types.BoolValue(false),
216+
},
217+
plan: Project{
218+
Version: types.Int64Value(1),
219+
EnableSearchIndexCustomers: types.BoolValue(true),
220+
},
221+
action: platform.ProjectUpdate{
222+
Version: 1,
223+
Actions: []platform.ProjectUpdateAction{
224+
platform.ProjectChangeCustomerSearchStatusAction{Status: platform.CustomerSearchStatusActivated},
225+
},
226+
},
227+
},
228+
{
229+
name: "Update with search index customers deactivated",
230+
state: Project{
231+
Version: types.Int64Value(1),
232+
EnableSearchIndexCustomers: types.BoolValue(true),
233+
},
234+
plan: Project{
235+
Version: types.Int64Value(1),
236+
EnableSearchIndexCustomers: types.BoolValue(false),
237+
},
238+
action: platform.ProjectUpdate{
239+
Version: 1,
240+
Actions: []platform.ProjectUpdateAction{
241+
platform.ProjectChangeCustomerSearchStatusAction{Status: platform.CustomerSearchStatusDeactivated},
242+
},
243+
},
244+
},
245+
{
246+
name: "Update with search index customers no changes",
247+
state: Project{
248+
Version: types.Int64Value(1),
249+
EnableSearchIndexCustomers: types.BoolValue(false),
250+
},
251+
plan: Project{
252+
Version: types.Int64Value(1),
253+
EnableSearchIndexCustomers: types.BoolValue(false),
254+
},
255+
action: platform.ProjectUpdate{
256+
Version: 1,
257+
Actions: []platform.ProjectUpdateAction{},
258+
},
259+
},
260+
{
261+
name: "Update with search index products activated",
262+
state: Project{
263+
Version: types.Int64Value(1),
264+
EnableSearchIndexProducts: types.BoolValue(false),
265+
},
266+
plan: Project{
267+
Version: types.Int64Value(1),
268+
EnableSearchIndexProducts: types.BoolValue(true),
269+
},
270+
action: platform.ProjectUpdate{
271+
Version: 1,
272+
Actions: []platform.ProjectUpdateAction{
273+
platform.ProjectChangeProductSearchIndexingEnabledAction{
274+
Enabled: true,
275+
Mode: utils.GetRef(platform.ProductSearchIndexingModeProductProjectionsSearch),
276+
},
277+
},
278+
},
279+
},
280+
{
281+
name: "Update with search index products deactivated",
282+
state: Project{
283+
Version: types.Int64Value(1),
284+
EnableSearchIndexProducts: types.BoolValue(true),
285+
},
286+
plan: Project{
287+
Version: types.Int64Value(1),
288+
EnableSearchIndexProducts: types.BoolValue(false),
289+
},
290+
action: platform.ProjectUpdate{
291+
Version: 1,
292+
Actions: []platform.ProjectUpdateAction{
293+
platform.ProjectChangeProductSearchIndexingEnabledAction{
294+
Enabled: false,
295+
Mode: utils.GetRef(platform.ProductSearchIndexingModeProductProjectionsSearch),
296+
},
297+
},
298+
},
299+
},
300+
{
301+
name: "Update with search index product search activated",
302+
state: Project{
303+
Version: types.Int64Value(1),
304+
EnableSearchIndexProductSearch: types.BoolValue(false),
305+
},
306+
plan: Project{
307+
Version: types.Int64Value(1),
308+
EnableSearchIndexProductSearch: types.BoolValue(true),
309+
},
310+
action: platform.ProjectUpdate{
311+
Version: 1,
312+
Actions: []platform.ProjectUpdateAction{
313+
platform.ProjectChangeProductSearchIndexingEnabledAction{
314+
Enabled: true,
315+
Mode: utils.GetRef(platform.ProductSearchIndexingModeProductsSearch),
316+
},
317+
},
318+
},
319+
},
320+
{
321+
name: "Update with search index product search deactivated",
322+
state: Project{
323+
Version: types.Int64Value(1),
324+
EnableSearchIndexProductSearch: types.BoolValue(true),
325+
},
326+
plan: Project{
327+
Version: types.Int64Value(1),
328+
EnableSearchIndexProductSearch: types.BoolValue(false),
329+
},
330+
action: platform.ProjectUpdate{
331+
Version: 1,
332+
Actions: []platform.ProjectUpdateAction{
333+
platform.ProjectChangeProductSearchIndexingEnabledAction{
334+
Enabled: false,
335+
Mode: utils.GetRef(platform.ProductSearchIndexingModeProductsSearch),
336+
},
337+
},
338+
},
339+
},
179340
{
180341
name: "Create with business unit settings",
181342
state: Project{

internal/resources/project/resource.go

+18-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package project
22

33
import (
44
"context"
5+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
56
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
67
"time"
78

@@ -18,7 +19,7 @@ import (
1819
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
1920
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
2021
"github.com/hashicorp/terraform-plugin-framework/types"
21-
sdk_resource "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
22+
sdkresource "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
2223
"github.com/labd/commercetools-go-sdk/platform"
2324

2425
"github.com/labd/terraform-provider-commercetools/internal/customtypes"
@@ -114,20 +115,28 @@ func (r *projectResource) Schema(_ context.Context, _ resource.SchemaRequest, re
114115
},
115116
},
116117
"enable_search_index_products": schema.BoolAttribute{
118+
MarkdownDescription: "Enable the Search Indexing of product projections",
119+
Optional: true,
120+
Computed: true,
121+
Default: booldefault.StaticBool(false),
122+
},
123+
"enable_search_index_product_search": schema.BoolAttribute{
117124
MarkdownDescription: "Enable the Search Indexing of products",
118125
Optional: true,
119126
Computed: true,
120-
PlanModifiers: []planmodifier.Bool{
121-
boolplanmodifier.UseStateForUnknown(),
122-
},
127+
Default: booldefault.StaticBool(false),
123128
},
124129
"enable_search_index_orders": schema.BoolAttribute{
125130
MarkdownDescription: "Enable the Search Indexing of orders",
126131
Optional: true,
127132
Computed: true,
128-
PlanModifiers: []planmodifier.Bool{
129-
boolplanmodifier.UseStateForUnknown(),
130-
},
133+
Default: booldefault.StaticBool(false),
134+
},
135+
"enable_search_index_customers": schema.BoolAttribute{
136+
MarkdownDescription: "Enable the Search Indexing of customers",
137+
Optional: true,
138+
Computed: true,
139+
Default: booldefault.StaticBool(false),
131140
},
132141
"shipping_rate_input_type": schema.StringAttribute{
133142
MarkdownDescription: "Three ways to dynamically select a ShippingRatePriceTier exist. The CartValue type uses " +
@@ -327,7 +336,7 @@ func (r *projectResource) Create(ctx context.Context, req resource.CreateRequest
327336
}
328337

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

404413
var res *platform.Project
405-
err = sdk_resource.RetryContext(ctx, 5*time.Second, func() *sdk_resource.RetryError {
414+
err = sdkresource.RetryContext(ctx, 5*time.Second, func() *sdkresource.RetryError {
406415
var err error
407416
res, err = r.client.Post(input).Execute(ctx)
408417
return utils.ProcessRemoteError(err)

0 commit comments

Comments
 (0)