Skip to content

Commit

Permalink
feat: Fixed document update reponse processing
Browse files Browse the repository at this point in the history
  • Loading branch information
adanylenko committed Mar 22, 2024
1 parent c0b7ee7 commit 4929a77
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 4 deletions.
48 changes: 48 additions & 0 deletions docs/resources/document.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_document Resource - typesense"
subcategory: ""
description: |-
Every record you index in Typesense is called a Document
---

# typesense_document (Resource)

Every record you index in Typesense is called a Document

## Example Usage

```terraform
resource "typesense_document" "my-document" {
name = "test-document"
collection_name = typesense_collection.test_collection.name
document = <<EOF
{
"field1":"testValue1",
"field2":"testValue2"
}
EOF
}
```

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

### Required

- `collection_name` (String) Collection name
- `document` (String) Document object in JSON format
- `name` (String) Name identifier, it will be used as id, so needs to be URL-friendly

### Read-Only

- `id` (String) Id identifier

## Import

Import is supported using the following syntax:

```shell
terraform import typesense_document.my_document document-id
```
14 changes: 14 additions & 0 deletions examples/provider-install-verification/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,17 @@ resource "typesense_synonym" "test" {
synonyms = ["updated1", "value2", "value3"]

}

resource "typesense_document" "test" {
name = "test-document-2"
collection_name = typesense_collection.test_collection.name

document = <<EOF
{
"ronati_product_height_imp":"testValue1Updated5",
"test_field":"testValue2_2V4",
"newField1":"newFieldValue1",
"newField2":"newFieldValue2"
}
EOF
}
13 changes: 11 additions & 2 deletions internal/provider/resource_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ func (r *DocumentResource) Read(ctx context.Context, req resource.ReadRequest, r
if err != nil {
if strings.Contains(err.Error(), "Not Found") {
resp.State.RemoveResource(ctx)
resp.Diagnostics.AddWarning("Resource Not Found", fmt.Sprintf("Unable to retrieve document, got error: %s", err))
} else {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to retrieve document, got error: %s", err))
}
Expand Down Expand Up @@ -195,8 +196,15 @@ func (r *DocumentResource) Update(ctx context.Context, req resource.UpdateReques
_ = result // result is empty

if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to update document, got error: %s", err))
return

//check if error contains 201 response
if strings.Contains(err.Error(), "201") {
//ignore, sometimes typesense returns 201 code
} else {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to update document, got error: %s", err))
return
}

}

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
Expand All @@ -219,6 +227,7 @@ func (r *DocumentResource) Delete(ctx context.Context, req resource.DeleteReques
if err != nil {
if strings.Contains(err.Error(), "Not Found") {
resp.State.RemoveResource(ctx)
resp.Diagnostics.AddWarning("Resource Not Found", fmt.Sprintf("Unable to delete document, got error: %s", err))
} else {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete document, got error: %s", err))
}
Expand Down
5 changes: 3 additions & 2 deletions internal/provider/resource_synonym.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (r *SynonymResource) Create(ctx context.Context, req resource.CreateRequest
synonym, err := r.client.Collection(data.CollectionName.ValueString()).Synonyms().Upsert(ctx, data.Name.ValueString(), schema)

if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create collection, got error: %s", err))
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create synonym, got error: %s", err))
return
}

Expand All @@ -151,6 +151,7 @@ func (r *SynonymResource) Read(ctx context.Context, req resource.ReadRequest, re
if err != nil {
if strings.Contains(err.Error(), "Not Found") {
resp.State.RemoveResource(ctx)
resp.Diagnostics.AddWarning("Resource Not Found", fmt.Sprintf("Unable to find synonym %s, removing from state", data.Id.ValueString()))
} else {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to retrieve synonym, got error: %s", err))
}
Expand Down Expand Up @@ -190,7 +191,7 @@ func (r *SynonymResource) Update(ctx context.Context, req resource.UpdateRequest
synonym, err := r.client.Collection(data.CollectionName.ValueString()).Synonyms().Upsert(ctx, data.Id.ValueString(), schema)

if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create collection, got error: %s", err))
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create synonym, got error: %s", err))
return
}

Expand Down

0 comments on commit 4929a77

Please sign in to comment.