Skip to content
This repository has been archived by the owner on Oct 5, 2024. It is now read-only.

Commit

Permalink
move provider
Browse files Browse the repository at this point in the history
  • Loading branch information
bchilcott committed Aug 20, 2024
1 parent 6f4f79d commit 07c82a4
Show file tree
Hide file tree
Showing 10 changed files with 703 additions and 6 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module terraform-provider-turso
module github.com/bchilcott/terraform-provider-turso

go 1.21.0

Expand Down
3 changes: 2 additions & 1 deletion internal/provider/database_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package provider
import (
"context"
"fmt"
"terraform-provider-turso/internal/client"

"github.com/bchilcott/terraform-provider-turso/internal/client"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
Expand Down
3 changes: 2 additions & 1 deletion internal/provider/database_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
"context"
"fmt"
"strings"
"terraform-provider-turso/internal/client"

"github.com/bchilcott/terraform-provider-turso/internal/client"

"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
Expand Down
3 changes: 2 additions & 1 deletion internal/provider/database_token_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package provider
import (
"context"
"fmt"
"terraform-provider-turso/internal/client"

"github.com/bchilcott/terraform-provider-turso/internal/client"

"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
Expand Down
3 changes: 2 additions & 1 deletion internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
"context"
"fmt"
"net/http"
"terraform-provider-turso/internal/client"

"github.com/bchilcott/terraform-provider-turso/internal/client"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/diag"
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
"context"
"flag"
"log"
"terraform-provider-turso/internal/provider"

"github.com/bchilcott/terraform-provider-turso/internal/provider"

"github.com/hashicorp/terraform-plugin-framework/providerserver"
)
Expand Down
132 changes: 132 additions & 0 deletions provider/database_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package provider

import (
"context"
"fmt"

"github.com/bchilcott/terraform-provider-turso/internal/client"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)

var _ datasource.DataSource = &DatabaseDataSource{}

func NewDatabaseDataSource() datasource.DataSource {
return &DatabaseDataSource{}
}

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

// DatabaseDataSourceModel describes the data source data model.
type DatabaseDataSourceModel struct {
OrganizationName types.String `tfsdk:"organization_name"`
Name types.String `tfsdk:"name"`
Group types.String `tfsdk:"group"`
SizeLimit types.String `tfsdk:"size_limit"`
IsSchema types.Bool `tfsdk:"is_schema"`
Schema types.String `tfsdk:"schema"`

// Computed
DbId types.String `tfsdk:"db_id"`
Hostname types.String `tfsdk:"hostname"`
}

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

func (d *DatabaseDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Database data source",

Attributes: map[string]schema.Attribute{
"organization_name": schema.StringAttribute{
MarkdownDescription: "Name of organization to create the database for",
Required: true,
},
"name": schema.StringAttribute{
MarkdownDescription: "The name of the new database. Must contain only lowercase letters, numbers, dashes. No longer than 32 characters.",
Required: true,
},
"group": schema.StringAttribute{
MarkdownDescription: "The name of the group where the database should be created. The group must already exist.",
Optional: true,
},
"size_limit": schema.StringAttribute{
MarkdownDescription: "The maximum size of the database in bytes. Values with units are also accepted, e.g. 1mb, 256mb, 1gb.",
Optional: true,
},
"is_schema": schema.BoolAttribute{
MarkdownDescription: "Mark this database as the parent schema database that updates child databases with any schema changes.",
Optional: true,
},
"schema": schema.StringAttribute{
MarkdownDescription: "The name of the parent database to use as the schema.",
Optional: true,
},
"db_id": schema.StringAttribute{
MarkdownDescription: "The database universal unique identifier (UUID).",
Computed: true,
},
"hostname": schema.StringAttribute{
MarkdownDescription: "The DNS hostname used for client libSQL and HTTP connections.",
Computed: true,
},
},
}
}

func (d *DatabaseDataSource) 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.(*client.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 *DatabaseDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data DatabaseDataSourceModel

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

if resp.Diagnostics.HasError() {
return
}

res, err := d.client.GetDatabase(ctx, client.GetDatabaseParams{
OrganizationName: data.OrganizationName.ValueString(),
DatabaseName: data.Name.ValueString(),
})

if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read database, got error: %s", err.Error()))
return
}

switch p := res.(type) {
case *client.GetDatabaseOK:
data.DbId = types.StringValue(string(p.Database.Value.DbId.Value)) //nolint:all
data.Hostname = types.StringValue(string(p.Database.Value.Hostname.Value)) //nolint:all
}

// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
Loading

0 comments on commit 07c82a4

Please sign in to comment.