Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
Khyme committed Dec 7, 2023
1 parent 9bc78bb commit fce1446
Show file tree
Hide file tree
Showing 12 changed files with 492 additions and 451 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package provider
package products

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package provider
package products_test

import (
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/timescale/terraform-provider-timescale/internal/test"
)

func TestProductDataSource(t *testing.T) {
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: test.TestAccProtoV6ProviderFactories,
PreCheck: func() { test.TestAccPreCheck(t) },
Steps: []resource.TestStep{
// Read datasource
{
Expand All @@ -24,7 +25,7 @@ func TestProductDataSource(t *testing.T) {
}

func newProductsConfig() string {
return providerConfig + `
return test.ProviderConfig + `
data "timescale_products" "products" {
}`
}
83 changes: 83 additions & 0 deletions internal/provider/datasource/service/service_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package service

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-log/tflog"

tsClient "github.com/timescale/terraform-provider-timescale/internal/client"
sc "github.com/timescale/terraform-provider-timescale/internal/schema"
)

// Ensure provider defined types fully satisfy framework interfaces.
var _ datasource.DataSource = &ServiceDataSource{}
var _ datasource.DataSourceWithConfigure = &ServiceDataSource{}

func NewServiceDataSource() datasource.DataSource {
return &ServiceDataSource{}
}

// ServiceDataSource defines the data source implementation.
type ServiceDataSource struct {
client *tsClient.Client
}

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

func (d *ServiceDataSource) 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: "Service data source",
Attributes: sc.Converter(sc.Service()),
}
}

func (d *ServiceDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
tflog.Trace(ctx, "ServiceDataSource.Configure")

if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*tsClient.Client)

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

d.client = client
}

func (d *ServiceDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
tflog.Trace(ctx, "ServiceDataSource.Read")

var id string
resp.Diagnostics.Append(req.Config.GetAttribute(ctx, path.Root("id"), &id)...)

if resp.Diagnostics.HasError() {
tflog.Error(ctx, fmt.Sprintf("error reading terraform plan %v", resp.Diagnostics.Errors()))
return
}

tflog.Info(ctx, "Getting Service: "+id)
service, err := d.client.GetService(ctx, id)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read service, got error: %s", err))
return
}
state := sc.NewService(ctx, service, &resp.Diagnostics, nil)
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
if resp.Diagnostics.HasError() {
tflog.Error(ctx, fmt.Sprintf("error updating terraform state %v", resp.Diagnostics.Errors()))
return
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package provider
package service_test

import (
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/timescale/terraform-provider-timescale/internal/test"
)

func TestServiceDataSource(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
ProtoV6ProviderFactories: test.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Read testing
{
Expand All @@ -31,7 +32,7 @@ func TestServiceDataSource(t *testing.T) {
}

func newServiceDataSource() string {
return providerConfig + `
return test.ProviderConfig + `
resource "timescale_service" "resource" {
name = "newServiceDataSource test"
}
Expand Down
File renamed without changes.
12 changes: 8 additions & 4 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import (
"github.com/hashicorp/terraform-plugin-log/tflog"

tsClient "github.com/timescale/terraform-provider-timescale/internal/client"
d_products "github.com/timescale/terraform-provider-timescale/internal/provider/datasource/products"
d_service "github.com/timescale/terraform-provider-timescale/internal/provider/datasource/service"
d_vpc "github.com/timescale/terraform-provider-timescale/internal/provider/datasource/vpc"
r_service "github.com/timescale/terraform-provider-timescale/internal/provider/resource/service"
)

// Ensure TimescaleProvider satisfies various provider interfaces.
Expand Down Expand Up @@ -120,17 +124,17 @@ func (p *TimescaleProvider) Configure(ctx context.Context, req provider.Configur
func (p *TimescaleProvider) Resources(ctx context.Context) []func() resource.Resource {
tflog.Trace(ctx, "TimescaleProvider.Resources")
return []func() resource.Resource{
NewServiceResource,
r_service.NewServiceResource,
}
}

// DataSources defines the data sources implemented in the provider.
func (p *TimescaleProvider) DataSources(ctx context.Context) []func() datasource.DataSource {
tflog.Trace(ctx, "TimescaleProvider.DataSources")
return []func() datasource.DataSource{
NewProductsDataSource,
NewServiceDataSource,
NewVpcsDataSource,
d_products.NewProductsDataSource,
d_service.NewServiceDataSource,
d_vpc.NewVpcsDataSource,
}
}

Expand Down
Loading

0 comments on commit fce1446

Please sign in to comment.