From 6f32569b14e119a84f86302681a33393a64f9d6a Mon Sep 17 00:00:00 2001 From: Alexander Danylenko Date: Wed, 27 Mar 2024 15:19:34 +0200 Subject: [PATCH] feat: Fixed import for synonyms and objects --- internal/provider/resource_document.go | 30 ++++++++++++++++++++----- internal/provider/resource_synonym.go | 31 ++++++++++++++++++++------ internal/provider/util.go | 15 +++++++++++++ 3 files changed, 63 insertions(+), 13 deletions(-) diff --git a/internal/provider/resource_document.go b/internal/provider/resource_document.go index 56826e5..89c3b1a 100644 --- a/internal/provider/resource_document.go +++ b/internal/provider/resource_document.go @@ -121,7 +121,7 @@ func (r *DocumentResource) Create(ctx context.Context, req resource.CreateReques return } - data.Id = types.StringValue(result["id"].(string)) + data.Id = types.StringValue(createId(data.CollectionName.ValueString(), result["id"].(string))) delete(result, "id") @@ -145,7 +145,13 @@ func (r *DocumentResource) Read(ctx context.Context, req resource.ReadRequest, r return } - result, err := r.client.Collection(data.CollectionName.ValueString()).Document(data.Id.ValueString()).Retrieve(ctx) + collectionName, id, parseError := splitCollectionRelatedId(data.Id.ValueString()) + if parseError != nil { + resp.Diagnostics.AddError("Invalid ID", fmt.Sprintf("Unable to split resource ID: %s", parseError)) + return + } + + result, err := r.client.Collection(collectionName).Document(id).Retrieve(ctx) if err != nil { if strings.Contains(err.Error(), "Not Found") { @@ -158,7 +164,7 @@ func (r *DocumentResource) Read(ctx context.Context, req resource.ReadRequest, r return } - data.Id = types.StringValue(result["id"].(string)) + // data.Id = types.StringValue(result["id"].(string)) data.Name = types.StringValue(result["id"].(string)) delete(result, "id") @@ -190,9 +196,15 @@ func (r *DocumentResource) Update(ctx context.Context, req resource.UpdateReques return } - document["id"] = data.Id.ValueString() + collectionName, id, parseError := splitCollectionRelatedId(data.Id.ValueString()) + if parseError != nil { + resp.Diagnostics.AddError("Invalid ID", fmt.Sprintf("Unable to split resource ID: %s", parseError)) + return + } - result, err := r.client.Collection(data.CollectionName.ValueString()).Document(data.Id.ValueString()).Update(ctx, document) + document["id"] = id + + result, err := r.client.Collection(collectionName).Document(id).Update(ctx, document) _ = result // result is empty if err != nil { @@ -220,9 +232,15 @@ func (r *DocumentResource) Delete(ctx context.Context, req resource.DeleteReques return } + collectionName, id, parseError := splitCollectionRelatedId(data.Id.ValueString()) + if parseError != nil { + resp.Diagnostics.AddError("Invalid ID", fmt.Sprintf("Unable to split resource ID: %s", parseError)) + return + } + tflog.Warn(ctx, "###Delete Document with id="+data.Id.ValueString()) - _, err := r.client.Collection(data.CollectionName.ValueString()).Document(data.Id.ValueString()).Delete(ctx) + _, err := r.client.Collection(collectionName).Document(id).Delete(ctx) if err != nil { if strings.Contains(err.Error(), "Not Found") { diff --git a/internal/provider/resource_synonym.go b/internal/provider/resource_synonym.go index d26048c..abdc60c 100644 --- a/internal/provider/resource_synonym.go +++ b/internal/provider/resource_synonym.go @@ -128,8 +128,7 @@ func (r *SynonymResource) Create(ctx context.Context, req resource.CreateRequest return } - data.Id = types.StringPointerValue(synonym.Id) - data.Name = types.StringPointerValue(synonym.Id) + data.Id = types.StringValue(createId(data.CollectionName.ValueString(), *synonym.Id)) data.Root = types.StringPointerValue(synonym.Root) data.Synonyms = convertStringArrayToTerraformArray(synonym.Synonyms) @@ -146,7 +145,13 @@ func (r *SynonymResource) Read(ctx context.Context, req resource.ReadRequest, re return } - synonym, err := r.client.Collection(data.CollectionName.ValueString()).Synonym(data.Id.ValueString()).Retrieve(ctx) + collectionName, id, parseError := splitCollectionRelatedId(data.Id.ValueString()) + if parseError != nil { + resp.Diagnostics.AddError("Invalid ID", fmt.Sprintf("Unable to split resource ID: %s", parseError)) + return + } + + synonym, err := r.client.Collection(collectionName).Synonym(id).Retrieve(ctx) if err != nil { if strings.Contains(err.Error(), "Not Found") { @@ -159,7 +164,7 @@ func (r *SynonymResource) Read(ctx context.Context, req resource.ReadRequest, re return } - data.Id = types.StringPointerValue(synonym.Id) + // data.Id = types.StringPointerValue(synonym.Id) data.Name = types.StringPointerValue(synonym.Id) data.Synonyms = convertStringArrayToTerraformArray(synonym.Synonyms) @@ -188,14 +193,20 @@ func (r *SynonymResource) Update(ctx context.Context, req resource.UpdateRequest tflog.Info(ctx, "synonyms: "+fmt.Sprint(schema.Synonyms)) tflog.Info(ctx, "collection name: "+data.CollectionName.ValueString()) - synonym, err := r.client.Collection(data.CollectionName.ValueString()).Synonyms().Upsert(ctx, data.Id.ValueString(), schema) + collectionName, id, parseError := splitCollectionRelatedId(data.Id.ValueString()) + if parseError != nil { + resp.Diagnostics.AddError("Invalid ID", fmt.Sprintf("Unable to split resource ID: %s", parseError)) + return + } + + synonym, err := r.client.Collection(collectionName).Synonyms().Upsert(ctx, id, schema) if err != nil { resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create synonym, got error: %s", err)) return } - data.Id = types.StringPointerValue(synonym.Id) + // data.Id = types.StringPointerValue(synonym.Id) data.Name = types.StringPointerValue(synonym.Id) data.Root = types.StringPointerValue(synonym.Root) data.Synonyms = convertStringArrayToTerraformArray(synonym.Synonyms) @@ -215,7 +226,13 @@ func (r *SynonymResource) Delete(ctx context.Context, req resource.DeleteRequest tflog.Warn(ctx, "###Delete Synonym with id="+data.Id.ValueString()) - _, err := r.client.Collection(data.CollectionName.ValueString()).Synonym(data.Id.ValueString()).Delete(ctx) + collectionName, id, parseError := splitCollectionRelatedId(data.Id.ValueString()) + if parseError != nil { + resp.Diagnostics.AddError("Invalid ID", fmt.Sprintf("Unable to split resource ID: %s", parseError)) + return + } + + _, err := r.client.Collection(collectionName).Synonym(id).Delete(ctx) if err != nil { if strings.Contains(err.Error(), "Not Found") { diff --git a/internal/provider/util.go b/internal/provider/util.go index 09f9267..5087352 100644 --- a/internal/provider/util.go +++ b/internal/provider/util.go @@ -2,6 +2,8 @@ package provider import ( "encoding/json" + "fmt" + "strings" "github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes" "github.com/hashicorp/terraform-plugin-framework/types" @@ -43,3 +45,16 @@ func parseMapToJsonString(data map[string]interface{}) (jsontypes.Normalized, er } return jsontypes.NewNormalizedValue(string(jsonBytes)), nil } + +func splitCollectionRelatedId(input string) (string, string, error) { + eles := strings.Split(input, ".") + if len(eles) != 2 { + return "", "", fmt.Errorf("invalid format, format should be .") + } + + return eles[0], eles[1], nil +} + +func createId(collection string, resource string) string { + return fmt.Sprintf("%s.%s", collection, resource) +}