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

added instances data source #40

Merged
merged 1 commit into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions docs/data-sources/instances.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "pinot_instances Data Source - terraform-provider-pinot"
subcategory: ""
description: |-

---

# pinot_instances (Data Source)





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

### Read-Only

- `instances` (Attributes List) The list of instances. (see [below for nested schema](#nestedatt--instances))

<a id="nestedatt--instances"></a>
### Nested Schema for `instances`

Read-Only:

- `admin_port` (Number) The admin port of the instance.
- `enabled` (Boolean) If the instance is enabled.
- `grpc_port` (Number) The GRPC port of the instance.
- `host_name` (String) The hostname of the instance.
- `instance_name` (String) The name of the instance.
- `pools` (List of String) The list of pools.
- `port` (String) The port of the instance.
- `query_mailbox_port` (Number) The query mailbox port of the instance.
- `query_service_port` (Number) The query server port of the instance.
- `system_resource_info` (Attributes) The role of the user. (see [below for nested schema](#nestedatt--instances--system_resource_info))
- `tags` (List of String) The list of tags.

<a id="nestedatt--instances--system_resource_info"></a>
### Nested Schema for `instances.system_resource_info`

Read-Only:

- `max_heap_size_mb` (String) The max heap size in MB.
- `num_cores` (String) The number of cores.
- `total_memory_mb` (String) The total memory in MB.
18 changes: 18 additions & 0 deletions examples/instances/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
terraform {
required_providers {
pinot = {
source = "hashicorp.com/edu/pinot"
}
}
}

provider "pinot" {
controller_url = "http://localhost:9000"
auth_token = "YWRtaW46dmVyeXNlY3JldA"
}

data "pinot_instances" "test" {}

output "test_instances" {
value = data.pinot_instances.test
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 // indirect
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/armon/go-radix v1.0.0 // indirect
github.com/azaurus1/go-pinot-api v0.1.5-0.20240315010733-84469b9bf04d // indirect
github.com/azaurus1/go-pinot-api v0.1.5-0.20240317032613-e4cce989debc // indirect
github.com/bgentry/speakeasy v0.1.0 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/fatih/color v1.16.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ github.com/azaurus1/go-pinot-api v0.1.5-0.20240313083242-aed39a4025dc h1:7lAK2XB
github.com/azaurus1/go-pinot-api v0.1.5-0.20240313083242-aed39a4025dc/go.mod h1:uSL8T9cQCVP5U0MKBKCYuntMpIqjvZRgGk/epgSAQgw=
github.com/azaurus1/go-pinot-api v0.1.5-0.20240315010733-84469b9bf04d h1:zZ3TFC4H4olE2m0+/W/EfR+lwC+8lniG8nnrcoocN4U=
github.com/azaurus1/go-pinot-api v0.1.5-0.20240315010733-84469b9bf04d/go.mod h1:uSL8T9cQCVP5U0MKBKCYuntMpIqjvZRgGk/epgSAQgw=
github.com/azaurus1/go-pinot-api v0.1.5-0.20240317032613-e4cce989debc h1:K0fFGdFhUOHF8AI1slHBPI1rpxGv3eSqCwb8mMzzieQ=
github.com/azaurus1/go-pinot-api v0.1.5-0.20240317032613-e4cce989debc/go.mod h1:7WldcdBEA0ceY36yhiYrDmSt4h9cvMPAJw6BSZtQFjI=
github.com/bgentry/speakeasy v0.1.0 h1:ByYyxL9InA1OWqxJqqp2A5pYHUrCiAL6K3J+LKSsQkY=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bufbuild/protocompile v0.4.0 h1:LbFKd2XowZvQ/kajzguUp2DC9UEIQhIq77fZZlaQsNA=
Expand Down
199 changes: 199 additions & 0 deletions internal/provider/instances_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
package provider

import (
"context"
"fmt"

goPinotAPI "github.com/azaurus1/go-pinot-api"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
)

// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &instancesDataSource{}
_ datasource.DataSourceWithConfigure = &instancesDataSource{}
)

// NewUsersDataSource is a helper function to simplify the provider implementation.
func NewInstancesDataSource() datasource.DataSource {
return &instancesDataSource{}
}

// usersDataSource is the data source implementation.
type instancesDataSource struct {
client *goPinotAPI.PinotAPIClient
}

type instancesDataSourceModel struct {
Instances []instancesModel `tfsdk:"instances"`
}

type systemResourceInfoModel struct {
NumCores string `tfsdk:"num_cores"`
TotalMemoryMB string `tfsdk:"total_memory_mb"`
MaxHeapSizeMB string `tfsdk:"max_heap_size_mb"`
}

type instancesModel struct {
InstanceName string `tfsdk:"instance_name"`
HostName string `tfsdk:"host_name"`
Enabled bool `tfsdk:"enabled"`
Port string `tfsdk:"port"`
Tags []string `tfsdk:"tags"`
Pools []string `tfsdk:"pools"`
GRPCPort int `tfsdk:"grpc_port"`
AdminPort int `tfsdk:"admin_port"`
QueryServicePort int `tfsdk:"query_service_port"`
QueryMailboxPort int `tfsdk:"query_mailbox_port"`
SystemResourceInfo systemResourceInfoModel `tfsdk:"system_resource_info"`
}

// Configure adds the provider configured client to the data source.
func (d *instancesDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}

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

return
}

d.client = client
}

// Metadata returns the data source type name.
func (d *instancesDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_instances"
}

// Schema defines the schema for the data source.
func (d *instancesDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"instances": schema.ListNestedAttribute{
Description: "The list of instances.",
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"instance_name": schema.StringAttribute{
Description: "The name of the instance.",
Computed: true,
},
"host_name": schema.StringAttribute{
Description: "The hostname of the instance.",
Computed: true,
// Sensitive: true,
},
"enabled": schema.BoolAttribute{
Description: "If the instance is enabled.",
Computed: true,
},
"port": schema.StringAttribute{
Description: "The port of the instance.",
Computed: true,
},
"tags": schema.ListAttribute{
Description: "The list of tags.",
Computed: true,
ElementType: basetypes.StringType{},
},
"pools": schema.ListAttribute{
Description: "The list of pools.",
Computed: true,
ElementType: basetypes.StringType{},
},
"grpc_port": schema.NumberAttribute{
Description: "The GRPC port of the instance.",
Computed: true,
},
"admin_port": schema.NumberAttribute{
Description: "The admin port of the instance.",
Computed: true,
},
"query_service_port": schema.NumberAttribute{
Description: "The query server port of the instance.",
Computed: true,
},
"query_mailbox_port": schema.NumberAttribute{
Description: "The query mailbox port of the instance.",
Computed: true,
},
"system_resource_info": schema.SingleNestedAttribute{
Description: "The role of the user.",
Computed: true,
Attributes: map[string]schema.Attribute{
"num_cores": schema.StringAttribute{
Description: "The number of cores.",
Computed: true,
},
"total_memory_mb": schema.StringAttribute{
Description: "The total memory in MB.",
Computed: true,
},
"max_heap_size_mb": schema.StringAttribute{
Description: "The max heap size in MB.",
Computed: true,
},
},
},
},
},
},
},
}
}

// Read refreshes the Terraform state with the latest data.
func (d *instancesDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state instancesDataSourceModel

instancesResp, err := d.client.GetInstances()
if err != nil {
resp.Diagnostics.AddError("Failed to get instances", fmt.Sprintf("Failed to get instances: %s", err))
return
}

for _, instance := range instancesResp.Instances {
instanceResp, err := d.client.GetInstance(instance)
if err != nil {
resp.Diagnostics.AddError("Failed to get instance", fmt.Sprintf("Failed to get instance: %s", err))
return
}

systemInfo := systemResourceInfoModel{
NumCores: instanceResp.SystemResourceInfo.NumCores,
TotalMemoryMB: instanceResp.SystemResourceInfo.TotalMemoryMB,
MaxHeapSizeMB: instanceResp.SystemResourceInfo.MaxHeapSizeMB,
}

state.Instances = append(state.Instances, instancesModel{
InstanceName: instanceResp.InstanceName,
HostName: instanceResp.Hostname,
Enabled: instanceResp.Enabled,
Port: instanceResp.Port,
Tags: instanceResp.Tags,
Pools: instanceResp.Pools,
GRPCPort: instanceResp.GRPCPort,
AdminPort: instanceResp.AdminPort,
QueryServicePort: instanceResp.QueryServicePort,
QueryMailboxPort: instanceResp.QueryMailboxPort,
SystemResourceInfo: systemInfo,
})

}

diags := resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

}
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ func (p *pinotProvider) DataSources(_ context.Context) []func() datasource.DataS
NewSegmentsDataSource,
NewTenantsDataSource,
NewClustersDataSource,
NewInstancesDataSource,
}
}

Expand Down
Loading