Skip to content

Commit

Permalink
Fix collection data source read
Browse files Browse the repository at this point in the history
Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com>
  • Loading branch information
KeisukeYamashita committed Nov 3, 2021
1 parent e40d997 commit 48009c9
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion typesense/data_source_collection.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package typesense

import (
"context"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/typesense/typesense-go/typesense"
)

func dataSourceTypesenseCollection() *schema.Resource {
return &schema.Resource{
ReadContext: resourceTypesenseCollectionRead,
Description: "Group of related documents which are roughly equivalent to a table in a relational database.",
Schema: map[string]*schema.Schema{
"name": {
Expand Down Expand Up @@ -55,5 +59,38 @@ func dataSourceTypesenseCollection() *schema.Resource {
Computed: true,
},
},
ReadContext: dataSourceTypesenseCollectionRead,
}
}

func dataSourceTypesenseCollectionRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*typesense.Client)

var diags diag.Diagnostics

collection, err := client.Collection(d.Get("name").(string)).Retrieve()
if err != nil {
d.SetId("")
return diag.FromErr(err)
}

log.Printf("[DEBUG] Got collection name:%s\n", collection.Name)

if err := d.Set("name", collection.Name); err != nil {
return diag.FromErr(err)
}

if err := d.Set("fields", flattenCollectionFields(collection.Fields)); err != nil {
return diag.FromErr(err)
}

if err := d.Set("default_sorting_field", collection.DefaultSortingField); err != nil {
return diag.FromErr(err)
}

if err := d.Set("num_documents", collection.NumDocuments); err != nil {
return diag.FromErr(err)
}

return diags
}

0 comments on commit 48009c9

Please sign in to comment.