Skip to content

Commit

Permalink
fix(doc): Fixed doc generation and added examples for collection
Browse files Browse the repository at this point in the history
  • Loading branch information
adanylenko committed Mar 8, 2024
1 parent 203cf2d commit be38edd
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ jobs:
- name: Build app
run: make build
- name: Test app
run: make test
run: make testacc
2 changes: 1 addition & 1 deletion .github/workflows/semver-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 ./...
Expand Down
3 changes: 2 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
```

Expand Down
33 changes: 32 additions & 1 deletion docs/resources/collection.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 generated by tfplugindocs -->
## Schema
Expand Down Expand Up @@ -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
```
5 changes: 3 additions & 2 deletions examples/provider/provider.tf
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions examples/resources/typesense_collection/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
terraform import typesense_collection.my_collection my-collection
20 changes: 20 additions & 0 deletions examples/resources/typesense_collection/resource.tf
Original file line number Diff line number Diff line change
@@ -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"
}
}
29 changes: 16 additions & 13 deletions internal/provider/resource_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
),
},
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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())
}

Expand All @@ -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)
Expand All @@ -307,15 +310,15 @@ 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)...)

if resp.Diagnostics.HasError() {
return
}

tflog.Warn(ctx, "###Delete collection with id="+data.Id.ValueString())

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

data.Id = types.StringValue("")
Expand Down

0 comments on commit be38edd

Please sign in to comment.