Skip to content

Commit

Permalink
Merge pull request #65 from k-yomo/fix-virtual-index-setting
Browse files Browse the repository at this point in the history
Add 'virtual' param not to try to update unsupported params for virtual replica index
  • Loading branch information
k-yomo authored Feb 23, 2022
2 parents 131bf2e + f896e65 commit 8f319f7
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 57 deletions.
1 change: 1 addition & 0 deletions docs/data-sources/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ data "algolia_index" "example" {
- **query_strategy_config** (List of Object) The configuration for query strategy in index setting. (see [below for nested schema](#nestedatt--query_strategy_config))
- **ranking_config** (List of Object) The configuration for ranking. (see [below for nested schema](#nestedatt--ranking_config))
- **typos_config** (List of Object) The configuration for typos in index setting. (see [below for nested schema](#nestedatt--typos_config))
- **virtual** (Boolean) Whether the index is virtual index.

<a id="nestedatt--advanced_config"></a>
### Nested Schema for `advanced_config`
Expand Down
4 changes: 4 additions & 0 deletions docs/resources/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ resource "algolia_index" "example" {
- **timeouts** (Block, Optional) (see [below for nested schema](#nestedblock--timeouts))
- **typos_config** (Block List, Max: 1) The configuration for typos in index setting. (see [below for nested schema](#nestedblock--typos_config))

### Read-Only

- **virtual** (Boolean) Whether the index is virtual index. If true, applying the params listed in the [doc](https://www.algolia.com/doc/guides/managing-results/refine-results/sorting/in-depth/replicas/#unsupported-parameters) will be ignored.

<a id="nestedblock--advanced_config"></a>
### Nested Schema for `advanced_config`

Expand Down
5 changes: 5 additions & 0 deletions internal/provider/data_source_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ func dataSourceIndex() *schema.Resource {
ForceNew: true,
Description: "Name of the index.",
},
"virtual": {
Type: schema.TypeBool,
Computed: true,
Description: "Whether the index is virtual index.",
},
"attributes_config": {
Type: schema.TypeList,
Computed: true,
Expand Down
1 change: 1 addition & 0 deletions internal/provider/data_source_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func TestAccDataSourceIndex(t *testing.T) {
Config: testAccDatasourceIndex(indexName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "name", indexName),
resource.TestCheckResourceAttr(dataSourceName, "virtual", "false"),
testCheckResourceListAttr(dataSourceName, "attributes_config.0.searchable_attributes", []string{"title", "category,tag", "unordered(description)"}),
testCheckResourceListAttr(dataSourceName, "attributes_config.0.attributes_for_faceting", []string{"category"}),
testCheckResourceListAttr(dataSourceName, "attributes_config.0.unretrievable_attributes", []string{"author_email"}),
Expand Down
2 changes: 0 additions & 2 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import (
"context"

"github.com/algolia/algoliasearch-client-go/v3/algolia/region"

"github.com/algolia/algoliasearch-client-go/v3/algolia/search"
"github.com/algolia/algoliasearch-client-go/v3/algolia/suggestions"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand Down
141 changes: 86 additions & 55 deletions internal/provider/resource_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ func resourceIndex() *schema.Resource {
ForceNew: true,
Description: "Name of the index.",
},
"virtual": {
Type: schema.TypeBool,
Computed: true,
Description: "Whether the index is virtual index. If true, applying the params listed in the [doc](https://www.algolia.com/doc/guides/managing-results/refine-results/sorting/in-depth/replicas/#unsupported-parameters) will be ignored.",
},
"attributes_config": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -681,8 +686,14 @@ func refreshIndexState(ctx context.Context, d *schema.ResourceData, m interface{
})
}

isVirtualIndex := false
if primaryIndex := settings.Primary.Get(); primaryIndex != "" {
isVirtualIndex = true
}

values := map[string]interface{}{
"name": d.Id(),
"name": d.Id(),
"virtual": isVirtualIndex,
"attributes_config": []interface{}{map[string]interface{}{
"searchable_attributes": settings.SearchableAttributes.Get(),
"attributes_for_faceting": settings.AttributesForFaceting.Get(),
Expand Down Expand Up @@ -768,9 +779,11 @@ func refreshIndexState(ctx context.Context, d *schema.ResourceData, m interface{
}

func mapToIndexSettings(d *schema.ResourceData) search.Settings {
isVirtualIndex := d.Get("virtual").(bool)

settings := search.Settings{}
if v, ok := d.GetOk("attributes_config"); ok {
unmarshalAttributesConfig(v, &settings)
unmarshalAttributesConfig(v, &settings, isVirtualIndex)
}
if v, ok := d.GetOk("ranking_config"); ok {
unmarshalRankingConfig(v, &settings)
Expand All @@ -785,10 +798,10 @@ func mapToIndexSettings(d *schema.ResourceData) search.Settings {
unmarshalPaginationConfig(v, &settings)
}
if v, ok := d.GetOk("typos_config"); ok {
unmarshalTyposConfig(v, &settings)
unmarshalTyposConfig(v, &settings, isVirtualIndex)
}
if v, ok := d.GetOk("languages_config"); ok {
unmarshalLanguagesConfig(v, &settings)
unmarshalLanguagesConfig(v, &settings, isVirtualIndex)
}
if v, ok := d.GetOk("enable_rules"); ok {
settings.EnableRules = opt.EnableRules(v.(bool))
Expand All @@ -797,28 +810,30 @@ func mapToIndexSettings(d *schema.ResourceData) search.Settings {
settings.EnablePersonalization = opt.EnablePersonalization(v.(bool))
}
if v, ok := d.GetOk("query_strategy_config"); ok {
unmarshalQueryStrategyConfig(v, &settings)
unmarshalQueryStrategyConfig(v, &settings, isVirtualIndex)
}
if v, ok := d.GetOk("performance_config"); ok {
unmarshalPerformanceConfig(v, &settings)
unmarshalPerformanceConfig(v, &settings, isVirtualIndex)
}
if v, ok := d.GetOk("advanced_config"); ok {
unmarshalAdvancedConfig(v, &settings)
unmarshalAdvancedConfig(v, &settings, isVirtualIndex)
}

return settings
}

func unmarshalAttributesConfig(configured interface{}, settings *search.Settings) {
func unmarshalAttributesConfig(configured interface{}, settings *search.Settings, isVirtualIndex bool) {
l := configured.([]interface{})
if len(l) == 0 || l[0] == nil {
return
}
config := l[0].(map[string]interface{})
settings.SearchableAttributes = opt.SearchableAttributes(castStringList(config["searchable_attributes"])...)
settings.AttributesForFaceting = opt.AttributesForFaceting(castStringSet(config["attributes_for_faceting"])...)
settings.UnretrievableAttributes = opt.UnretrievableAttributes(castStringSet(config["unretrievable_attributes"])...)
settings.AttributesToRetrieve = opt.AttributesToRetrieve(castStringSet(config["attributes_to_retrieve"])...)
if !isVirtualIndex {
settings.SearchableAttributes = opt.SearchableAttributes(castStringList(config["searchable_attributes"])...)
settings.AttributesForFaceting = opt.AttributesForFaceting(castStringSet(config["attributes_for_faceting"])...)
}
}

func unmarshalRankingConfig(configured interface{}, settings *search.Settings) {
Expand Down Expand Up @@ -891,7 +906,7 @@ func unmarshalPaginationConfig(configured interface{}, settings *search.Settings
}
}

func unmarshalTyposConfig(configured interface{}, settings *search.Settings) {
func unmarshalTyposConfig(configured interface{}, settings *search.Settings, isVirtualIndex bool) {
l := configured.([]interface{})
if len(l) == 0 || l[0] == nil {
return
Expand Down Expand Up @@ -920,18 +935,21 @@ func unmarshalTyposConfig(configured interface{}, settings *search.Settings) {
if v, ok := config["allow_typos_on_numeric_tokens"]; ok {
settings.AllowTyposOnNumericTokens = opt.AllowTyposOnNumericTokens(v.(bool))
}
if v, ok := config["disable_typo_tolerance_on_attributes"]; ok {
settings.DisableTypoToleranceOnAttributes = opt.DisableTypoToleranceOnAttributes(castStringList(v)...)
}
if v, ok := config["disable_typo_tolerance_on_words"]; ok {
settings.DisableTypoToleranceOnWords = opt.DisableTypoToleranceOnWords(castStringList(v)...)
}
if v, ok := config["separators_to_index"]; ok {
settings.SeparatorsToIndex = opt.SeparatorsToIndex(v.(string))

if !isVirtualIndex {
if v, ok := config["disable_typo_tolerance_on_attributes"]; ok {
settings.DisableTypoToleranceOnAttributes = opt.DisableTypoToleranceOnAttributes(castStringList(v)...)
}
if v, ok := config["disable_typo_tolerance_on_words"]; ok {
settings.DisableTypoToleranceOnWords = opt.DisableTypoToleranceOnWords(castStringList(v)...)
}
if v, ok := config["separators_to_index"]; ok {
settings.SeparatorsToIndex = opt.SeparatorsToIndex(v.(string))
}
}
}

func unmarshalLanguagesConfig(configured interface{}, settings *search.Settings) {
func unmarshalLanguagesConfig(configured interface{}, settings *search.Settings, isVirtualIndex bool) {
l := configured.([]interface{})
if len(l) == 0 || l[0] == nil {
return
Expand All @@ -948,9 +966,6 @@ func unmarshalLanguagesConfig(configured interface{}, settings *search.Settings)
settings.IgnorePlurals = opt.IgnorePluralsFor(set...)
}
}
if v, ok := config["attributes_to_transliterate"]; ok {
settings.AttributesToTransliterate = opt.AttributesToTransliterate(castStringSet(v)...)
}
if v, ok := config["remove_stop_words"]; ok {
settings.RemoveStopWords = opt.RemoveStopWords(v.(bool))
}
Expand All @@ -960,27 +975,35 @@ func unmarshalLanguagesConfig(configured interface{}, settings *search.Settings)
settings.RemoveStopWords = opt.RemoveStopWordsFor(set...)
}
}
if v, ok := config["camel_case_attributes"]; ok {
settings.CamelCaseAttributes = opt.CamelCaseAttributes(castStringSet(v)...)
}
if v, ok := config["decompounded_attributes"]; ok {
unmarshalLanguagesConfigDecompoundedAttributes(v, settings)
}
if v, ok := config["keep_diacritics_on_characters"]; ok {
settings.KeepDiacriticsOnCharacters = opt.KeepDiacriticsOnCharacters(v.(string))
}
if v, ok := config["custom_normalization"]; ok {
settings.CustomNormalization = opt.CustomNormalization(map[string]map[string]string{"default": castStringMap(v)})
}
if v, ok := config["query_languages"]; ok {
settings.QueryLanguages = opt.QueryLanguages(castStringSet(v)...)
}
if v, ok := config["index_languages"]; ok {
settings.IndexLanguages = opt.IndexLanguages(castStringSet(v)...)
}
if v, ok := config["decompound_query"]; ok {
settings.DecompoundQuery = opt.DecompoundQuery(v.(bool))
}
if !isVirtualIndex {
if v, ok := config["attributes_to_transliterate"]; ok {
settings.AttributesToTransliterate = opt.AttributesToTransliterate(castStringSet(v)...)
}
if v, ok := config["camel_case_attributes"]; ok {
settings.CamelCaseAttributes = opt.CamelCaseAttributes(castStringSet(v)...)
}
if v, ok := config["keep_diacritics_on_characters"]; ok {
settings.KeepDiacriticsOnCharacters = opt.KeepDiacriticsOnCharacters(v.(string))
}
if v, ok := config["decompounded_attributes"]; ok {
unmarshalLanguagesConfigDecompoundedAttributes(v, settings)
}
if v, ok := config["custom_normalization"]; ok {
settings.CustomNormalization = opt.CustomNormalization(map[string]map[string]string{"default": castStringMap(v)})
}
if v, ok := config["index_languages"]; ok {
settings.IndexLanguages = opt.IndexLanguages(castStringSet(v)...)
}
}
}

func unmarshalLanguagesConfigDecompoundedAttributes(configured interface{}, settings *search.Settings) {
Expand All @@ -998,7 +1021,7 @@ func unmarshalLanguagesConfigDecompoundedAttributes(configured interface{}, sett
settings.DecompoundedAttributes = opt.DecompoundedAttributes(decompoundedAttributesMap)
}

func unmarshalQueryStrategyConfig(configured interface{}, settings *search.Settings) {
func unmarshalQueryStrategyConfig(configured interface{}, settings *search.Settings, isVirtualIndex bool) {
l := configured.([]interface{})
if len(l) == 0 || l[0] == nil {
return
Expand All @@ -1015,15 +1038,6 @@ func unmarshalQueryStrategyConfig(configured interface{}, settings *search.Setti
if v, ok := config["advanced_syntax"]; ok {
settings.AdvancedSyntax = opt.AdvancedSyntax(v.(bool))
}
if v, ok := config["optional_words"]; ok {
settings.OptionalWords = opt.OptionalWords(castStringSet(v)...)
}
if v, ok := config["disable_prefix_on_attributes"]; ok {
settings.DisablePrefixOnAttributes = opt.DisablePrefixOnAttributes(castStringSet(v)...)
}
if v, ok := config["disable_exact_on_attributes"]; ok {
settings.DisableExactOnAttributes = opt.DisableExactOnAttributes(castStringSet(v)...)
}
if v, ok := config["exact_on_single_word_query"]; ok {
settings.ExactOnSingleWordQuery = opt.ExactOnSingleWordQuery(v.(string))
}
Expand All @@ -1033,35 +1047,46 @@ func unmarshalQueryStrategyConfig(configured interface{}, settings *search.Setti
if v, ok := config["advanced_syntax_features"]; ok {
settings.AdvancedSyntaxFeatures = opt.AdvancedSyntaxFeatures(castStringSet(v)...)
}

if !isVirtualIndex {
if v, ok := config["optional_words"]; ok {
settings.OptionalWords = opt.OptionalWords(castStringSet(v)...)
}
if v, ok := config["disable_prefix_on_attributes"]; ok {
settings.DisablePrefixOnAttributes = opt.DisablePrefixOnAttributes(castStringSet(v)...)
}
if v, ok := config["disable_exact_on_attributes"]; ok {
settings.DisableExactOnAttributes = opt.DisableExactOnAttributes(castStringSet(v)...)
}
}
}

func unmarshalPerformanceConfig(configured interface{}, settings *search.Settings) {
func unmarshalPerformanceConfig(configured interface{}, settings *search.Settings, isVirtualIndex bool) {
l := configured.([]interface{})
if len(l) == 0 || l[0] == nil {
return
}

config := l[0].(map[string]interface{})

if v, ok := config["numeric_attributes_for_filtering"]; ok {
settings.NumericAttributesForFiltering = opt.NumericAttributesForFiltering(castStringSet(v)...)
}
if v, ok := config["allow_compression_of_integer_array"]; ok {
settings.AllowCompressionOfIntegerArray = opt.AllowCompressionOfIntegerArray(v.(bool))
if !isVirtualIndex {
if v, ok := config["numeric_attributes_for_filtering"]; ok {
settings.NumericAttributesForFiltering = opt.NumericAttributesForFiltering(castStringSet(v)...)
}
if v, ok := config["allow_compression_of_integer_array"]; ok {
settings.AllowCompressionOfIntegerArray = opt.AllowCompressionOfIntegerArray(v.(bool))
}
}
}

func unmarshalAdvancedConfig(configured interface{}, settings *search.Settings) {
func unmarshalAdvancedConfig(configured interface{}, settings *search.Settings, isVirtualIndex bool) {
l := configured.([]interface{})
if len(l) == 0 || l[0] == nil {
return
}

config := l[0].(map[string]interface{})

if v, ok := config["attribute_for_distinct"]; ok {
settings.AttributeForDistinct = opt.AttributeForDistinct(v.(string))
}
if v, ok := config["distinct"]; ok {
settings.Distinct = opt.DistinctOf(v.(int))
}
Expand All @@ -1080,4 +1105,10 @@ func unmarshalAdvancedConfig(configured interface{}, settings *search.Settings)
if v, ok := config["attribute_criteria_computed_by_min_proximity"]; ok {
settings.AttributeCriteriaComputedByMinProximity = opt.AttributeCriteriaComputedByMinProximity(v.(bool))
}

if !isVirtualIndex {
if v, ok := config["attribute_for_distinct"]; ok {
settings.AttributeForDistinct = opt.AttributeForDistinct(v.(string))
}
}
}
3 changes: 3 additions & 0 deletions internal/provider/resource_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
)

// TODO: Cover all params
// TODO: Add a test for virtual index (virtual index can't be created on current Free plan)
func TestAccResourceIndex(t *testing.T) {
indexName := randStringStartWithAlpha(100)
resourceName := fmt.Sprintf("algolia_index.%s", indexName)
Expand All @@ -22,6 +23,7 @@ func TestAccResourceIndex(t *testing.T) {
Config: testAccResourceIndex(indexName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "name", indexName),
resource.TestCheckResourceAttr(resourceName, "virtual", "false"),
resource.TestCheckNoResourceAttr(resourceName, "attributes_config.0.searchable_attributes.0"),
resource.TestCheckNoResourceAttr(resourceName, "attributes_config.0.attributes_for_faceting.0"),
resource.TestCheckNoResourceAttr(resourceName, "attributes_config.0.unretrievable_attributes.0"),
Expand All @@ -47,6 +49,7 @@ func TestAccResourceIndex(t *testing.T) {
Config: testAccResourceIndexUpdate(indexName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "name", indexName),
resource.TestCheckResourceAttr(resourceName, "virtual", "false"),
testCheckResourceListAttr(resourceName, "attributes_config.0.searchable_attributes", []string{"title", "category,tag", "unordered(description)"}),
testCheckResourceListAttr(resourceName, "attributes_config.0.attributes_for_faceting", []string{"category"}),
testCheckResourceListAttr(resourceName, "attributes_config.0.unretrievable_attributes", []string{"author_email"}),
Expand Down

0 comments on commit 8f319f7

Please sign in to comment.