terraform-provider-edge-con.../internal/provider/app_data_source.go

112 lines
3.1 KiB
Go
Raw Normal View History

2025-11-11 14:15:52 +01:00
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"
2025-11-11 15:25:57 +01:00
"github.com/hashicorp/terraform-plugin-log/tflog"
2025-11-11 14:15:52 +01:00
edgeclient "edp.buildth.ing/DevFW-CICD/edge-connect-client/v2/sdk/edgeconnect/v2"
2025-11-11 14:15:52 +01:00
)
2025-11-11 15:25:57 +01:00
var _ datasource.DataSource = &AppDataSource{}
2025-11-11 14:15:52 +01:00
func NewAppDataSource() datasource.DataSource {
2025-11-11 15:25:57 +01:00
return &AppDataSource{}
2025-11-11 14:15:52 +01:00
}
2025-11-11 15:25:57 +01:00
type AppDataSource struct {
client *edgeclient.Client
2025-11-11 14:15:52 +01:00
}
2025-11-11 15:25:57 +01:00
type AppDataSourceModel struct {
Name types.String `tfsdk:"name"`
Organization types.String `tfsdk:"organization"`
Version types.String `tfsdk:"version"`
Region types.String `tfsdk:"region"`
2025-11-11 14:15:52 +01:00
}
2025-11-11 15:25:57 +01:00
func (d *AppDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
2025-11-11 14:15:52 +01:00
resp.TypeName = req.ProviderTypeName + "_app"
}
2025-11-11 15:25:57 +01:00
func (d *AppDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
2025-11-11 14:15:52 +01:00
resp.Schema = schema.Schema{
2025-11-11 15:25:57 +01:00
MarkdownDescription: "App data source",
2025-11-11 14:15:52 +01:00
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
MarkdownDescription: "App name (primary key)",
Required: true,
2025-11-11 14:15:52 +01:00
},
"organization": schema.StringAttribute{
MarkdownDescription: "Organization name (primary key)",
Required: true,
2025-11-11 14:15:52 +01:00
},
2025-11-11 15:25:57 +01:00
"version": schema.StringAttribute{
MarkdownDescription: "App version (primary key)",
Required: true,
2025-11-11 14:15:52 +01:00
},
"region": schema.StringAttribute{
MarkdownDescription: "Region (EU or US)",
Required: true,
2025-11-11 14:15:52 +01:00
},
},
}
}
2025-11-11 15:25:57 +01:00
func (d *AppDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
2025-11-11 14:15:52 +01:00
if req.ProviderData == nil {
return
}
2025-11-11 15:25:57 +01:00
client, ok := req.ProviderData.(*edgeclient.Client)
2025-11-11 14:15:52 +01:00
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
2025-11-11 15:25:57 +01:00
fmt.Sprintf("Expected *edgeclient.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
2025-11-11 14:15:52 +01:00
)
2025-11-11 15:25:57 +01:00
2025-11-11 14:15:52 +01:00
return
}
d.client = client
}
2025-11-11 15:25:57 +01:00
func (d *AppDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data AppDataSourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
2025-11-11 14:15:52 +01:00
if resp.Diagnostics.HasError() {
return
}
2025-11-11 15:25:57 +01:00
appKey := edgeclient.AppKey{
Organization: data.Organization.ValueString(),
Name: data.Name.ValueString(),
Version: data.Version.ValueString(),
2025-11-11 15:25:57 +01:00
}
2025-11-11 14:15:52 +01:00
region := data.Region.ValueString()
app, err := d.client.ShowApp(ctx, appKey, region)
2025-11-11 14:15:52 +01:00
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read app %s/%s/%s in region %s, got error: %s",
data.Organization.ValueString(), data.Name.ValueString(), data.Version.ValueString(), region, err))
2025-11-11 14:15:52 +01:00
return
}
2025-11-11 15:25:57 +01:00
data.Name = types.StringValue(app.Key.Name)
data.Organization = types.StringValue(app.Key.Organization)
2025-11-11 15:25:57 +01:00
data.Version = types.StringValue(app.Key.Version)
tflog.Trace(ctx, "read an app data source")
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
2025-11-11 14:15:52 +01:00
}