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

Auto generated data source for Database #7

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
115 changes: 115 additions & 0 deletions internal/provider/database_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package provider

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"
"github.com/madewithlove/forge-go-sdk"
)

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

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

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

// DatabaseDataSourceModel describes the data source data model.
type DatabaseDataSourceModel struct {
Id types.Int64 `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Status types.String `tfsdk:"status"`
CreatedAt types.String `tfsdk:"created_at"`
ServerId types.Int64 `tfsdk:"server_id"`
}

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{
// This description is used by the documentation generator and the language server.
MarkdownDescription: "Database data source",

Attributes: map[string]schema.Attribute{
"id": schema.Int64Attribute{
MarkdownDescription: "",
Required: true,
},
"name": schema.StringAttribute{
MarkdownDescription: "",
Computed: true,
},
"status": schema.StringAttribute{
MarkdownDescription: "",
Computed: true,
},
"created_at": schema.StringAttribute{
MarkdownDescription: "",
Computed: true,
},
"server_id": schema.Int64Attribute{
MarkdownDescription: "The ID of the server.",
Required: 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.(*forge.APIClient)

if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *forge.APIClient, 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
}

database, _, err := d.client.DefaultApi.GetDatabase(ctx, int32(data.ServerId.ValueInt64()), int32(data.Id.ValueInt64()))

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

data.Name = types.StringValue(database.Name)
data.Status = types.StringValue(database.Status)
data.CreatedAt = types.StringValue(database.CreatedAt)

// Write logs using the tflog package
// Documentation: https://terraform.io/plugin/log
tflog.Trace(ctx, "read a data source")

// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
37 changes: 37 additions & 0 deletions internal/provider/database_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package provider

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestAccDatabaseDataSource(t *testing.T) {
rnd := generateRandomResourceName()
name := "data.forge_database." + rnd

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Read testing
{
Config: testAccDatabaseDataSourceConfig(rnd),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(name, "id", "123"),
resource.TestCheckResourceAttr(name, "server_id", "123"),
),
},
},
})
}

func testAccDatabaseDataSourceConfig(resourceName string) string {
return fmt.Sprintf(`
data "forge_database" "%[1]s" {
id = "123"
server_id = "123"
}
`, resourceName)
}