From be38eddb4e0c60990f40dd99aff96ec964d84ad3 Mon Sep 17 00:00:00 2001 From: Alexander Danylenko Date: Fri, 8 Mar 2024 12:01:06 +0200 Subject: [PATCH] fix(doc): Fixed doc generation and added examples for collection --- .github/workflows/build-and-test.yml | 2 +- .github/workflows/semver-release.yml | 2 +- GNUmakefile | 2 +- docs/index.md | 3 +- docs/resources/collection.md | 33 ++++++++++++++++++- examples/provider/provider.tf | 5 +-- .../resources/typesense_collection/import.sh | 1 + .../typesense_collection/resource.tf | 20 +++++++++++ internal/provider/resource_collection.go | 29 ++++++++-------- 9 files changed, 77 insertions(+), 20 deletions(-) create mode 100644 examples/resources/typesense_collection/import.sh create mode 100644 examples/resources/typesense_collection/resource.tf diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index 88b4b34..2f184af 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -21,4 +21,4 @@ jobs: - name: Build app run: make build - name: Test app - run: make test + run: make testacc diff --git a/.github/workflows/semver-release.yml b/.github/workflows/semver-release.yml index f8c1128..e4f72d1 100644 --- a/.github/workflows/semver-release.yml +++ b/.github/workflows/semver-release.yml @@ -38,7 +38,7 @@ jobs: - name: Build app run: make build - name: Test app - run: make test + run: make testacc - name: Generate documentation run: make docs - name: Install dependencies diff --git a/GNUmakefile b/GNUmakefile index e927c83..d327d67 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -2,7 +2,7 @@ default: testacc # Run acceptance tests .PHONY: testacc -test: +testacc: TF_ACC=1 go test ./... -v $(TESTARGS) -timeout 120m doc: go generate ./... diff --git a/docs/index.md b/docs/index.md index 9ba959c..4add194 100644 --- a/docs/index.md +++ b/docs/index.md @@ -14,7 +14,8 @@ description: |- ```terraform provider "typesense" { - # example configuration here + api_key = "xxxxxxxxxxxxxxxxxx" // Or TYPESENSE_API_KEY enivoronment variable + api_address = "https://your.typesense.server" // Or TYPESENSE_APP_ADDRESS enivoronment variable } ``` diff --git a/docs/resources/collection.md b/docs/resources/collection.md index 701a1d6..b885374 100644 --- a/docs/resources/collection.md +++ b/docs/resources/collection.md @@ -10,7 +10,30 @@ description: |- Collection resource - +## Example Usage + +```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 + + fields { + facet = true + index = true + name = "testFiled1" + optional = true + type = "string" + } + + fields { + facet = true + index = true + name = "testFiled2" + optional = true + type = "int32" + } +} +``` ## Schema @@ -41,3 +64,11 @@ Optional: - `facet` (Boolean) - `index` (Boolean) Index field - `optional` (Boolean) Optional field + +## Import + +Import is supported using the following syntax: + +```shell +terraform import typesense_collection.my_collection my-collection +``` diff --git a/examples/provider/provider.tf b/examples/provider/provider.tf index f65ca9a..a5ff4b4 100644 --- a/examples/provider/provider.tf +++ b/examples/provider/provider.tf @@ -1,3 +1,4 @@ provider "typesense" { - # example configuration here -} + api_key = "xxxxxxxxxxxxxxxxxx" // Or TYPESENSE_API_KEY enivoronment variable + api_address = "https://your.typesense.server" // Or TYPESENSE_APP_ADDRESS enivoronment variable +} \ No newline at end of file diff --git a/examples/resources/typesense_collection/import.sh b/examples/resources/typesense_collection/import.sh new file mode 100644 index 0000000..fcd6ea2 --- /dev/null +++ b/examples/resources/typesense_collection/import.sh @@ -0,0 +1 @@ +terraform import typesense_collection.my_collection my-collection diff --git a/examples/resources/typesense_collection/resource.tf b/examples/resources/typesense_collection/resource.tf new file mode 100644 index 0000000..a9ffa51 --- /dev/null +++ b/examples/resources/typesense_collection/resource.tf @@ -0,0 +1,20 @@ +resource "typesense_collection" "my_collection" { + name = "my-collection" + default_sorting_field = "" //if not needed, should be set empty string to match Typesense collection schema + + fields { + facet = true + index = true + name = "testFiled1" + optional = true + type = "string" + } + + fields { + facet = true + index = true + name = "testFiled2" + optional = true + type = "int32" + } +} diff --git a/internal/provider/resource_collection.go b/internal/provider/resource_collection.go index c17073b..5e0efc7 100644 --- a/internal/provider/resource_collection.go +++ b/internal/provider/resource_collection.go @@ -58,9 +58,9 @@ func (r *CollectionResource) Schema(ctx context.Context, req resource.SchemaRequ "id": schema.StringAttribute{ Computed: true, MarkdownDescription: "Id identifier", - // PlanModifiers: []planmodifier.String{ - // stringplanmodifier.UseStateForUnknown(), - // }, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.UseStateForUnknown(), + }, }, "name": schema.StringAttribute{ MarkdownDescription: "Collection name", @@ -109,12 +109,16 @@ func (r *CollectionResource) Schema(ctx context.Context, req resource.SchemaRequ "int64", "float", "bool", + "geopoint", + "object", "string[]", "int32[]", "int64[]", "float[]", "bool[]", - "geopoint", + "geopoint[]", + "object[]", + "string*", "auto", ), }, @@ -194,8 +198,6 @@ func (r *CollectionResource) Read(ctx context.Context, req resource.ReadRequest, id := data.Id.ValueString() - tflog.Error(ctx, "Got collection id:"+id) - collection, err := r.client.Collection(id).Retrieve(ctx) if err != nil { @@ -204,7 +206,7 @@ func (r *CollectionResource) Read(ctx context.Context, req resource.ReadRequest, return } - tflog.Error(ctx, "Got collection name:"+collection.Name) + tflog.Info(ctx, "###Got collection name:"+collection.Name) data.Id = types.StringValue(collection.Name) data.Name = types.StringValue(collection.Name) @@ -262,7 +264,7 @@ func (r *CollectionResource) Update(ctx context.Context, req resource.UpdateRequ if _, ok := stateItems[field.Name.ValueString()]; !ok { schema.Fields = append(schema.Fields, filedModelToApiField(field)) - tflog.Warn(ctx, "###field will be created: "+field.Name.ValueString()) + tflog.Info(ctx, "###Field will be created: "+field.Name.ValueString()) } else if stateItems[field.Name.ValueString()] != field { //item was changed, need to update @@ -273,13 +275,14 @@ func (r *CollectionResource) Update(ctx context.Context, req resource.UpdateRequ Name: field.Name.ValueString(), }, filedModelToApiField(field)) - tflog.Warn(ctx, "###field will be updated: "+field.Name.ValueString()) + tflog.Info(ctx, "###Field will be updated: "+field.Name.ValueString()) } else { //item was not changed, do nothing - tflog.Warn(ctx, "###field remaining the same: "+field.Name.ValueString()) + tflog.Info(ctx, "###Field remaining the same: "+field.Name.ValueString()) } + //delete processed field from the state object delete(stateItems, field.Name.ValueString()) } @@ -289,7 +292,7 @@ func (r *CollectionResource) Update(ctx context.Context, req resource.UpdateRequ Drop: drop, Name: field.Name.ValueString(), }) - tflog.Warn(ctx, "###field will be deleted: "+field.Name.ValueString()) + tflog.Info(ctx, "###Field will be deleted: "+field.Name.ValueString()) } _, err := r.client.Collection(state.Id.ValueString()).Update(ctx, schema) @@ -307,8 +310,6 @@ func (r *CollectionResource) Update(ctx context.Context, req resource.UpdateRequ func (r *CollectionResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { var data CollectionResourceModel - tflog.Error(ctx, "delete collection") - // Read Terraform prior state data into the model resp.Diagnostics.Append(req.State.Get(ctx, &data)...) @@ -316,6 +317,8 @@ func (r *CollectionResource) Delete(ctx context.Context, req resource.DeleteRequ return } + tflog.Warn(ctx, "###Delete collection with id="+data.Id.ValueString()) + _, err := r.client.Collection(data.Id.ValueString()).Delete(ctx) data.Id = types.StringValue("")