-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(datasource):
pinecone_collection
- Loading branch information
1 parent
60c4d02
commit 041fbda
Showing
6 changed files
with
166 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
provider "pinecone" { | ||
apikey = var.pinecone_api_key | ||
environment = var.pinecone_environment | ||
} | ||
|
||
data "pinecone_collection" "existing-collection" { | ||
name = "testindex" | ||
} | ||
|
||
resource "pinecone_index" "my-first-index" { | ||
name = "testidx" | ||
metric = "cosine" | ||
pods = 1 | ||
|
||
source_collection = data.pinecone_collection.existing-collection.name | ||
# index and collection dimension must match | ||
dimension = data.pinecone_collection.existing-collection.dimension | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
terraform { | ||
required_providers { | ||
pinecone = { | ||
source = "thekevinwang.com/terraform-providers/pinecone" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
variable "pinecone_api_key" { | ||
type = string | ||
sensitive = true | ||
} | ||
|
||
variable "pinecone_environment" { | ||
type = string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package data_sources | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/datasource" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/hashicorp/terraform-plugin-log/tflog" | ||
|
||
services "github.com/thiskevinwang/terraform-provider-pinecone/internal/services" | ||
) | ||
|
||
// Ensure provider defined types fully satisfy framework interfaces. | ||
var ( | ||
_ datasource.DataSource = &CollectionDataSource{} | ||
) | ||
|
||
func NewCollectionDataSource() datasource.DataSource { | ||
return &CollectionDataSource{} | ||
} | ||
|
||
// CollectionDataSource defines the data source implementation. | ||
type CollectionDataSource struct { | ||
client services.Pinecone | ||
} | ||
|
||
// CollectionDataSourceModel describes the data source data model. | ||
type CollectionDataSourceModel struct { | ||
Name types.String `tfsdk:"name"` | ||
Dimension types.Int64 `tfsdk:"dimension"` | ||
Id types.String `tfsdk:"id"` | ||
} | ||
|
||
func (d *CollectionDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_collection" | ||
} | ||
|
||
func (d *CollectionDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
// This description is used by the documentation generator and the language server. | ||
MarkdownDescription: `A Pinecone collection | ||
- See [Understanding collections](https://docs.pinecone.io/docs/collections) | ||
- See [API Docs](https://docs.pinecone.io/reference/describe_collection) | ||
`, | ||
|
||
Attributes: map[string]schema.Attribute{ | ||
"name": schema.StringAttribute{ | ||
MarkdownDescription: "The name of the collection", | ||
Required: true, | ||
}, | ||
"dimension": schema.Int64Attribute{ | ||
MarkdownDescription: "The dimension of the collection", | ||
Required: false, | ||
Computed: true, | ||
}, | ||
"id": schema.StringAttribute{ | ||
MarkdownDescription: "Example identifier", | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
// Configure adds the provider configured client to the datasource | ||
func (d *CollectionDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { | ||
// Prevent panic if the provider has not been configured. | ||
if req.ProviderData == nil { | ||
return | ||
} | ||
|
||
// extract the client from the provider data | ||
client, ok := req.ProviderData.(services.Pinecone) | ||
|
||
if !ok { | ||
resp.Diagnostics.AddError( | ||
"Unexpected Resource Configure Type", | ||
fmt.Sprintf("Expected pinecone.Pinecone, got: %T", req.ProviderData), | ||
) | ||
|
||
return | ||
} | ||
|
||
d.client = client | ||
} | ||
|
||
func (d *CollectionDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { | ||
var data CollectionDataSourceModel | ||
|
||
// Read Terraform configuration data into the model | ||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) | ||
|
||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
// Generate API request body from configuration | ||
name := data.Name.ValueString() | ||
response, err := d.client.DescribeCollection(name) | ||
if err != nil { | ||
resp.Diagnostics.AddError( | ||
"Failed to describe collection", | ||
fmt.Sprintf("Failed to describe collection: %s", err), | ||
) | ||
return | ||
} | ||
|
||
// log the response | ||
tflog.Info(ctx, "DescribeCollection OK", map[string]any{"respond": *response}) | ||
|
||
data.Id = types.StringValue(fmt.Sprintf("datasource-pinecone_collection-%s/%s", d.client.Environment, name)) | ||
data.Name = types.StringValue(response.Name) | ||
data.Dimension = types.Int64Value(response.Dimension) | ||
|
||
// Save data into Terraform state | ||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters