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

111 lines
3.1 KiB
Go

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