Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to create a primary index using the couchbase-capella_query_indexes #259

Open
paivin-dn opened this issue Jan 24, 2025 · 2 comments
Open

Comments

@paivin-dn
Copy link

paivin-dn commented Jan 24, 2025

I try to create a primary index on a collection using this code

resource "couchbase-capella_query_indexes" "test-tf-primary" {
  organization_id = organization_id
  project_id      = project_id
  cluster_id      = cluster_id
  bucket_name     = bucket_name
  collection_name = collection_name
  is_primary      = true
  scope_name = scope_name
}

The index will be created, but the provider will get the following error

couchbase-capella_query_indexes.test-tf-primary: Creating...
╷
│ Error: Error reading query index
│ 
│   with couchbase-capella_query_indexes.test-tf-primary,
│   on main.tf line 203, in resource "couchbase-capella_query_indexes" "test-tf-primary":
│  203: resource "couchbase-capella_query_indexes" "test-tf-primary" {
│ 
│ Could not read query index : unexpected code: 404, expected: 200, body: 404 page not found
│ 
╵

I saw a doc in this PR, but It doesn't contain information about creating primary indexes

@dylanCz
Copy link

dylanCz commented Jan 31, 2025

We encountered this same issue and it looks like it's related to a bug in the provider code related to null checking the index name.

if !plan.IndexName.IsNull() {
	indexName = plan.IndexName.ValueString()
} else {
	indexName = "#primary"
}

A variation of this code block is repeated throughout the provider at various scopes which makes it hard to tell when indexName is actually null or not.

To resolve for now you can just modify your resource block, adding an index name:

resource "couchbase-capella_query_indexes" "test-tf-primary" {
  organization_id = organization_id
  project_id      = project_id
  cluster_id      = cluster_id
  bucket_name     = bucket_name
  collection_name = collection_name
  is_primary      = true
  scope_name      = scope_name
  index_name      = index_name
}

@cdsre
Copy link
Contributor

cdsre commented Jan 31, 2025

The crux of this issue is that In the create function the local variable indexName is set to either the value from the plan, or a default value. This then gets set in the ddl statement

			if !plan.IndexName.IsNull() {
				indexName = plan.IndexName.ValueString()
			} else {
				indexName = "#primary"
			}
			ddl = fmt.Sprintf(
				"CREATE PRIMARY INDEX `%s` ON `%s`.`%s`.`%s`  WITH { \"defer_build\": %t,  \"num_replica\": %d }",
				indexName,
				plan.BucketName.ValueString(),
				plan.ScopeName.ValueString(),
				plan.CollectionName.ValueString(),
				plan.With.DeferBuild.ValueBool(),
				plan.With.NumReplica.ValueInt64(),
			)

However later on in the create function after executing the statement there is a query of the index to get additional properties to enrich the state

	if state.BuildIndexes.IsNull() {
		index, err := g.getQueryIndex(
			ctx,
			state.OrganizationId.ValueString(),
			state.ProjectId.ValueString(),
			state.ClusterId.ValueString(),
			state.BucketName.ValueString(),
			state.ScopeName.ValueString(),
			state.CollectionName.ValueString(),
			state.IndexName.ValueString(),
		)
		if err != nil {
			resp.Diagnostics.AddError(
				"Error reading query index",
				"Could not read query index "+state.IndexName.ValueString()+": "+err.Error(),
			)
			return
		}

		state.Status = types.StringValue(index.Status)
		state.With.NumReplica = types.Int64Value(int64(index.NumReplica))
	}

However, the query here is using the value from the state which is just the plan values and this value is empty. Its not taking into account the fact that earlier in the function there was logic to set the indexName local var to #primary. We can validate this as @dylanCz said by just setting a name and it works with no issue.

We can also see that the error we should get here should include the index name before the colon but we just get an empty string

Could not read query index : 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants