123 lines
3.7 KiB
Go
123 lines
3.7 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"
|
|
)
|
|
|
|
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"`
|
|
}
|
|
|
|
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: "AppInstance data source",
|
|
|
|
Attributes: map[string]schema.Attribute{
|
|
"name": schema.StringAttribute{
|
|
MarkdownDescription: "AppInstance name (primary key)",
|
|
Required: true,
|
|
},
|
|
"organization": schema.StringAttribute{
|
|
MarkdownDescription: "Organization name (primary key)",
|
|
Required: true,
|
|
},
|
|
"cloudlet_name": schema.StringAttribute{
|
|
MarkdownDescription: "Cloudlet name (primary key)",
|
|
Required: true,
|
|
},
|
|
"cloudlet_organization": schema.StringAttribute{
|
|
MarkdownDescription: "Cloudlet organization (primary key)",
|
|
Required: true,
|
|
},
|
|
"region": schema.StringAttribute{
|
|
MarkdownDescription: "Region (EU or US) (primary key)",
|
|
Required: 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 AppInstanceDataSourceModel
|
|
|
|
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(),
|
|
},
|
|
}
|
|
|
|
region := data.Region.ValueString()
|
|
|
|
appInstance, err := d.client.ShowAppInstance(ctx, appInstKey, 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
|
|
}
|
|
|
|
data.Name = types.StringValue(appInstance.Key.Name)
|
|
data.AppId = types.StringValue(appInstance.AppKey.Name)
|
|
|
|
tflog.Trace(ctx, "read an app instance data source")
|
|
|
|
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
|
}
|