Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synonym implementation #42

Merged
merged 2 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions docs/resources/collection.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ Group of related documents which are roughly equivalent to a table in a relation

```terraform
resource "typesense_collection" "my_collection" {
name = "my-collection"
default_sorting_field = "" //if not needed, should be set empty string to match Typesense collection schema
name = "my-collection"

fields {
facet = true
Expand All @@ -40,11 +39,11 @@ resource "typesense_collection" "my_collection" {

### Required

- `default_sorting_field` (String) Default sorting field
- `name` (String) Collection name

### Optional

- `default_sorting_field` (String) Default sorting field
- `enable_nested_fields` (Boolean) Enable nested fields, must be enabled to use object/object[] types
- `fields` (Block List) (see [below for nested schema](#nestedblock--fields))

Expand Down
48 changes: 48 additions & 0 deletions docs/resources/synonym.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "typesense_synonym Resource - typesense"
subcategory: ""
description: |-
The synonyms feature allows you to define search terms that should be considered equivalent. For eg: when you define a synonym for sneaker as shoe, searching for sneaker will now return all records with the word shoe in them, in addition to records with the word sneaker.
---

# typesense_synonym (Resource)

The synonyms feature allows you to define search terms that should be considered equivalent. For eg: when you define a synonym for sneaker as shoe, searching for sneaker will now return all records with the word shoe in them, in addition to records with the word sneaker.

## Example Usage

```terraform
resource "typesense_synonym" "my_synonym" {
name = "smart-phone-synonym"
collection_name = typesense_collection.my_collection.name
root = "smart phone"

synonyms = ["iphone", "android"]
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `collection_name` (String) Collection name
- `name` (String) Name identifier
- `synonyms` (List of String) Array of words that should be considered as synonyms.

### Optional

- `root` (String) For 1-way synonyms, indicates the root word that words in the synonyms parameter map to

### Read-Only

- `id` (String) Id identifier

## Import

Import is supported using the following syntax:

```shell
terraform import typesense_synonym.my_synonym my-synonym
```
17 changes: 8 additions & 9 deletions examples/provider-install-verification/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ terraform {
provider "typesense" {
}
resource "typesense_collection" "test_collection" {
name = "adanylenko-test-collection-v2"
default_sorting_field = ""
name = "adanylenko-test-collection-v2"

fields {
facet = true
Expand All @@ -29,12 +28,12 @@ resource "typesense_collection" "test_collection" {
type = "string"
}

fields {
facet = true
index = true
name = "test_field_2_updated"
optional = true
type = "object"
}
}


resource "typesense_synonym" "test" {
name = "test"
collection_name = typesense_collection.test_collection.name
synonyms = ["updated1", "value2", "value3"]

}
3 changes: 1 addition & 2 deletions examples/resources/typesense_collection/resource.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
resource "typesense_collection" "my_collection" {
name = "my-collection"
default_sorting_field = "" //if not needed, should be set empty string to match Typesense collection schema
name = "my-collection"

fields {
facet = true
Expand Down
1 change: 1 addition & 0 deletions examples/resources/typesense_synonym/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
terraform import typesense_synonym.my_synonym my-synonym
7 changes: 7 additions & 0 deletions examples/resources/typesense_synonym/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
resource "typesense_synonym" "my_synonym" {
name = "my-synonym"
collection_name = typesense_collection.my_collection.name
root = "smart phone"

synonyms = ["iphone", "android"]
}
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func (p *TypesenseProvider) Configure(ctx context.Context, req provider.Configur
func (p *TypesenseProvider) Resources(_ context.Context) []func() resource.Resource {
return []func() resource.Resource{
NewCollectionResource,
NewSynonymResource,
}
}

Expand Down
34 changes: 26 additions & 8 deletions internal/provider/resource_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package provider
import (
"context"
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/path"
Expand Down Expand Up @@ -71,7 +72,7 @@ func (r *CollectionResource) Schema(ctx context.Context, req resource.SchemaRequ
},
},
"default_sorting_field": schema.StringAttribute{
Required: true,
Optional: true,
MarkdownDescription: "Default sorting field",
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
Expand Down Expand Up @@ -188,7 +189,11 @@ func (r *CollectionResource) Create(ctx context.Context, req resource.CreateRequ

data.Id = types.StringValue(collection.Name)
data.Name = types.StringValue(collection.Name)
data.DefaultSortingField = types.StringPointerValue(collection.DefaultSortingField)

if collection.DefaultSortingField != nil && *collection.DefaultSortingField != "" {
data.DefaultSortingField = types.StringPointerValue(collection.DefaultSortingField)
}

data.EnableNestedFields = types.BoolPointerValue(collection.EnableNestedFields)
data.Fields = flattenCollectionFields(collection.Fields)

Expand All @@ -210,16 +215,24 @@ func (r *CollectionResource) Read(ctx context.Context, req resource.ReadRequest,
collection, err := r.client.Collection(id).Retrieve(ctx)

if err != nil {
data.Id = types.StringValue("")
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to retrieve collection, got error: %s", err))
if strings.Contains(err.Error(), "Not Found") {
resp.State.RemoveResource(ctx)
} else {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to retrieve collection, got error: %s", err))
}

return
}

tflog.Info(ctx, "###Got collection name:"+collection.Name)

data.Id = types.StringValue(collection.Name)
data.Name = types.StringValue(collection.Name)
data.DefaultSortingField = types.StringPointerValue(collection.DefaultSortingField)

if collection.DefaultSortingField != nil && *collection.DefaultSortingField != "" {
data.DefaultSortingField = types.StringPointerValue(collection.DefaultSortingField)
}

data.EnableNestedFields = types.BoolPointerValue(collection.EnableNestedFields)
data.Fields = flattenCollectionFields(collection.Fields)

Expand Down Expand Up @@ -331,12 +344,17 @@ func (r *CollectionResource) Delete(ctx context.Context, req resource.DeleteRequ

_, err := r.client.Collection(data.Id.ValueString()).Delete(ctx)

data.Id = types.StringValue("")

if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete example, got error: %s", err))
if strings.Contains(err.Error(), "Not Found") {
resp.State.RemoveResource(ctx)
} else {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete collection, got error: %s", err))
}

return
}

data.Id = types.StringValue("")
}

func (r *CollectionResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
Expand Down
Loading
Loading