Skip to content

Commit

Permalink
bug fixes to read fxn and working delete
Browse files Browse the repository at this point in the history
  • Loading branch information
RaynorChavez committed Mar 1, 2024
1 parent 616a5e1 commit 627c596
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 29 deletions.
76 changes: 48 additions & 28 deletions internal/provider/indices_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package provider
import (
"context"
"fmt"
"strconv"
"terraform-provider-marqo/marqo"

"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)

// Ensure the implementation satisfies the expected interfaces.
Expand Down Expand Up @@ -156,48 +158,66 @@ func (r *indicesResource) Schema(_ context.Context, _ resource.SchemaRequest, re
}
}

// Utility function to convert standard Go string to types.Int64
func StringToInt64(str string) types.Int64 {
intVal, err := strconv.ParseInt(str, 10, 64)
if err != nil {
// Handle the error appropriately. Here, we return a Null types.Int64 to indicate failure.
return types.Int64Null()
}
return types.Int64Value(intVal)
}

func (r *indicesResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
// Initialize the state variable based on the IndexResourceModel
var state IndexResourceModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

indexSettings, err := r.marqoClient.GetIndexSettings(state.IndexName.ValueString())
tflog.Debug(ctx, "Calling marqo client ListIndices")
indices, err := r.marqoClient.ListIndices()
if err != nil {
resp.Diagnostics.AddError(
"Error Reading Index",
fmt.Sprintf("Could not read index '%s': %s", state.IndexName, err.Error()),
)
resp.Diagnostics.AddError("Failed to List Indices", fmt.Sprintf("Could not list indices: %s", err.Error()))
return
}

// Map the fetched index settings to the Terraform state
state.Settings = IndexSettingsModel{
Type: types.StringValue(indexSettings.Type),
VectorNumericType: types.StringValue(indexSettings.VectorNumericType),
TreatUrlsAndPointersAsImages: types.BoolValue(indexSettings.TreatUrlsAndPointersAsImages),
Model: types.StringValue(indexSettings.Model),
NormalizeEmbeddings: types.BoolValue(indexSettings.NormalizeEmbeddings),
TextPreprocessing: TextPreprocessingModelCreate{
SplitLength: types.Int64Value(indexSettings.TextPreprocessing.SplitLength),
SplitMethod: types.StringValue(indexSettings.TextPreprocessing.SplitMethod),
SplitOverlap: types.Int64Value(indexSettings.TextPreprocessing.SplitOverlap),
},
ImagePreprocessing: ImagePreprocessingModel{
PatchMethod: types.StringValue(indexSettings.ImagePreprocessing["patchMethod"].(string)),
},
AnnParameters: AnnParametersModelCreate{
SpaceType: types.StringValue(indexSettings.AnnParameters.SpaceType),
Parameters: ParametersModel{
EfConstruction: types.Int64Value(indexSettings.AnnParameters.Parameters.EfConstruction),
M: types.Int64Value(indexSettings.AnnParameters.Parameters.M),
},
},
FilterStringMaxLength: types.Int64Value(indexSettings.FilterStringMaxLength),
// Assuming you need to find a specific index based on the IndexName in the state
// and update the state with its settings. This is a simplification.
// In a real scenario, you might need to handle multiple indices or validate the existence.
for _, indexDetail := range indices {
if indexDetail.IndexName == state.IndexName.ValueString() {
// Update the state with the details from the indexDetail
state.Settings = IndexSettingsModel{
Type: types.StringValue(indexDetail.Type),
VectorNumericType: types.StringValue(indexDetail.VectorNumericType),
TreatUrlsAndPointersAsImages: types.BoolValue(indexDetail.TreatUrlsAndPointersAsImages),
Model: types.StringValue(indexDetail.Model),
NormalizeEmbeddings: types.BoolValue(indexDetail.NormalizeEmbeddings),
TextPreprocessing: TextPreprocessingModelCreate{
SplitLength: StringToInt64(indexDetail.TextPreprocessing.SplitLength),
SplitMethod: types.StringValue(indexDetail.TextPreprocessing.SplitMethod),
SplitOverlap: StringToInt64(indexDetail.TextPreprocessing.SplitOverlap),
},
ImagePreprocessing: ImagePreprocessingModel{
PatchMethod: types.StringValue(indexDetail.ImagePreprocessing.PatchMethod),
},
AnnParameters: AnnParametersModelCreate{
SpaceType: types.StringValue(indexDetail.AnnParameters.SpaceType),
Parameters: ParametersModel{
EfConstruction: StringToInt64(indexDetail.AnnParameters.Parameters.EfConstruction),
M: StringToInt64(indexDetail.AnnParameters.Parameters.M),
},
},
FilterStringMaxLength: StringToInt64(indexDetail.FilterStringMaxLength),
}
break
}
}

// Set the updated state
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand Down
4 changes: 3 additions & 1 deletion marqo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ func (c *Client) ListIndices() ([]IndexDetail, error) {
// GetIndexSettings fetches settings for a specific index and decodes into IndexSettings model
func (c *Client) GetIndexSettings(indexName string) (IndexSettings, error) {
url := fmt.Sprintf("%s/indexes/%s/settings", c.BaseURL, indexName)
fmt.Println("GetIndexSettings URL: ", url)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return IndexSettings{}, fmt.Errorf("API request error: %s - %v", req.URL.String(), err)
Expand All @@ -183,6 +184,7 @@ func (c *Client) GetIndexSettings(indexName string) (IndexSettings, error) {
if err != nil {
return IndexSettings{}, err
}
fmt.Println("Settings Response: ", resp)
defer resp.Body.Close()

var settings IndexSettings
Expand Down Expand Up @@ -240,7 +242,7 @@ func (c *Client) CreateIndex(indexName string, settings map[string]interface{})
if err != nil {
return err
}
//fmt.Println("Response: ", resp)
fmt.Println("Response: ", resp)
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
Expand Down

0 comments on commit 627c596

Please sign in to comment.