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

172 lines
5.8 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 = &AppInstanceDataSource{}
func NewAppInstanceDataSource() datasource.DataSource {
return &AppInstanceDataSource{}
}
type AppInstanceDataSource struct {
client *edgeclient.Client
}
type AppInstanceDataSourceModel struct {
Name types.String `tfsdk:"name"`
Organization types.String `tfsdk:"organization"`
CloudletName types.String `tfsdk:"cloudlet_name"`
CloudletOrganization types.String `tfsdk:"cloudlet_organization"`
Region types.String `tfsdk:"region"`
AppId types.String `tfsdk:"app_id"`
}
type AppInstancesDataSourceModel struct {
Name types.String `tfsdk:"name"`
Organization types.String `tfsdk:"organization"`
CloudletName types.String `tfsdk:"cloudlet_name"`
CloudletOrganization types.String `tfsdk:"cloudlet_organization"`
Region types.String `tfsdk:"region"`
AppId types.String `tfsdk:"app_id"`
AppInstances []AppInstanceDataSourceModel `tfsdk:"appinstances"`
}
func (d *AppInstanceDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_app_instance"
}
func (d *AppInstanceDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "AppInstances data source",
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
MarkdownDescription: "AppInstance name (primary key)",
Optional: true,
},
"organization": schema.StringAttribute{
MarkdownDescription: "Organization name (primary key)",
Optional: true,
},
"cloudlet_name": schema.StringAttribute{
MarkdownDescription: "Cloudlet name (primary key)",
Optional: true,
},
"cloudlet_organization": schema.StringAttribute{
MarkdownDescription: "Cloudlet organization (primary key)",
Optional: true,
},
"region": schema.StringAttribute{
MarkdownDescription: "Region (EU or US) (primary key)",
Required: true,
},
"app_id": schema.StringAttribute{
MarkdownDescription: "Associated App ID",
Optional: true,
},
"appinstances": schema.ListNestedAttribute{
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
MarkdownDescription: "AppInstance name (primary key)",
Computed: true,
},
"organization": schema.StringAttribute{
MarkdownDescription: "Organization name (primary key)",
Computed: true,
},
"cloudlet_name": schema.StringAttribute{
MarkdownDescription: "Cloudlet name (primary key)",
Computed: true,
},
"cloudlet_organization": schema.StringAttribute{
MarkdownDescription: "Cloudlet organization (primary key)",
Computed: true,
},
"region": schema.StringAttribute{
MarkdownDescription: "Region (EU or US) (primary key)",
Computed: true,
},
"app_id": schema.StringAttribute{
MarkdownDescription: "Associated App ID",
Computed: true,
},
},
},
},
},
}
}
func (d *AppInstanceDataSource) 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 *AppInstanceDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data AppInstancesDataSourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
appInstKey := edgeclient.AppInstanceKey{
Organization: data.Organization.ValueString(),
Name: data.Name.ValueString(),
CloudletKey: edgeclient.CloudletKey{
Organization: data.CloudletOrganization.ValueString(),
Name: data.CloudletName.ValueString(),
},
}
appKey := edgeclient.AppKey{
Name: data.AppId.ValueString(),
}
region := data.Region.ValueString()
appInstances, err := d.client.ShowAppInstances(ctx, appInstKey, appKey, region)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read app instance %s/%s in region %s, got error: %s",
data.Organization.ValueString(), data.Name.ValueString(), region, err))
return
}
for _, appInst := range appInstances {
data.AppInstances = append(data.AppInstances, AppInstanceDataSourceModel{
Name: types.StringValue(appInst.Key.Name),
Organization: types.StringValue(appInst.Key.Organization),
CloudletName: types.StringValue(appInst.Key.CloudletKey.Name),
CloudletOrganization: types.StringValue(appInst.Key.CloudletKey.Organization),
Region: types.StringValue(region),
AppId: types.StringValue(appInst.AppKey.Name),
})
}
tflog.Trace(ctx, "read app instances data source")
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}