Skip to content

Commit

Permalink
feat: query external id (#54)
Browse files Browse the repository at this point in the history
Signed-off-by: Yellow Shine <[email protected]>
  • Loading branch information
yellow-shine authored Feb 6, 2025
1 parent 565c8dd commit 7073b8a
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 1 deletion.
2 changes: 1 addition & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ unit-test:
go test ./... -v

install:
sudo go install -v ./...
go install -v ./...

doc:
go generate ./...
Expand Down
16 changes: 16 additions & 0 deletions client/byoc_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ func (c *Client) DescribeBYOCProject(params *DescribeBYOCProjectRequest) (*GetBY
return &response.Data, err
}

func (c *Client) GetExternalId() (string, error) {
var response zillizResponse[GetExternalIdResponse]
err := c.do("GET", "byoc/describe", nil, &response)
if err != nil {
return "", err
}
return response.Data.ExternalId, err
}

type GetExternalIdResponse struct {
OrgId string `json:"orgId"`
ExternalId string `json:"externalId"`
ServiceAccount string `json:"serviceAccount"`
Clouds []string `json:"clouds"`
}

type DescribeBYOCProjectRequest struct {
ProjectId string `json:"projectId"`
DataPlaneID string `json:"dataPlaneId"`
Expand Down
42 changes: 42 additions & 0 deletions docs/data-sources/external_id.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "zillizcloud_external_id Data Source - zillizcloud"
subcategory: ""
description: |-
---

# zillizcloud_external_id (Data Source)



## Example Usage

```terraform
terraform {
required_providers {
zillizcloud = {
source = "zilliztech/zillizcloud"
}
}
}
provider "zillizcloud" {
byoc_mode = true
}
data "zillizcloud_external_id" "current" {
}
output "external_id" {
value = data.zillizcloud_external_id.current.external_id
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Read-Only

- `external_id` (String) The external ID.
- `id` (String) The ID of the external ID.
18 changes: 18 additions & 0 deletions examples/data-sources/zillizcloud_external_id/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
terraform {
required_providers {
zillizcloud = {
source = "zilliztech/zillizcloud"
}
}
}

provider "zillizcloud" {
byoc_mode = true
}

data "zillizcloud_external_id" "current" {
}

output "external_id" {
value = data.zillizcloud_external_id.current.external_id
}
87 changes: 87 additions & 0 deletions internal/provider/byoc/external_id_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package byoc

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"

zilliz "github.com/zilliztech/terraform-provider-zillizcloud/client"
)

// ExternalIdDataSource defines the data source implementation.
type ExternalIdDataSource struct {
client *zilliz.Client
}

func NewExternalIdDataSource() datasource.DataSource {
return &ExternalIdDataSource{}
}

type ExternalIdDataSourceModel struct {
Id types.String `tfsdk:"id"`
ExternalId types.String `tfsdk:"external_id"`
}

func (d *ExternalIdDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_external_id"
}

func (d *ExternalIdDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
MarkdownDescription: "The ID of the external ID.",
Computed: true,
},
"external_id": schema.StringAttribute{
MarkdownDescription: "The external ID.",
Computed: true,
},
},
}
}

func (d *ExternalIdDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}

client, ok := req.ProviderData.(*zilliz.Client)

if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)

return
}

d.client = client
}

func (d *ExternalIdDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state ExternalIdDataSourceModel

// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &state)...)

if resp.Diagnostics.HasError() {
return
}

externalId, err := d.client.GetExternalId()
if err != nil {
resp.Diagnostics.AddError("Error getting external ID", err.Error())
return
}

state.Id = types.StringValue(externalId)
state.ExternalId = types.StringValue(externalId)

resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
}
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func (p *ZillizProvider) DataSources(ctx context.Context) []func() datasource.Da
NewProjectDataSource,
NewClustersDataSource,
NewClusterDataSource,
byoc.NewExternalIdDataSource,
}
}

Expand Down

0 comments on commit 7073b8a

Please sign in to comment.