123 lines
3.5 KiB
Go
123 lines
3.5 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 {
|
|
Id types.String `tfsdk:"id"`
|
|
Name types.String `tfsdk:"name"`
|
|
AppId types.String `tfsdk:"app_id"`
|
|
Description types.String `tfsdk:"description"`
|
|
Config types.String `tfsdk:"config"`
|
|
Status types.String `tfsdk:"status"`
|
|
}
|
|
|
|
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{
|
|
"id": schema.StringAttribute{
|
|
MarkdownDescription: "AppInstance identifier",
|
|
Required: true,
|
|
},
|
|
"name": schema.StringAttribute{
|
|
MarkdownDescription: "AppInstance name",
|
|
Computed: true,
|
|
},
|
|
"app_id": schema.StringAttribute{
|
|
MarkdownDescription: "Associated App ID",
|
|
Computed: true,
|
|
},
|
|
"description": schema.StringAttribute{
|
|
MarkdownDescription: "AppInstance description",
|
|
Computed: true,
|
|
},
|
|
"config": schema.StringAttribute{
|
|
MarkdownDescription: "AppInstance configuration",
|
|
Computed: true,
|
|
},
|
|
"status": schema.StringAttribute{
|
|
MarkdownDescription: "AppInstance status",
|
|
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: "default",
|
|
Name: data.Id.ValueString(),
|
|
CloudletKey: edgeclient.CloudletKey{
|
|
Organization: "default",
|
|
Name: "default-cloudlet",
|
|
},
|
|
}
|
|
|
|
appInstance, err := d.client.ShowAppInstance(ctx, appInstKey, "default")
|
|
if err != nil {
|
|
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read app instance %s, got error: %s", data.Id.ValueString(), err))
|
|
return
|
|
}
|
|
|
|
data.Name = types.StringValue(appInstance.Key.Name)
|
|
data.AppId = types.StringValue(appInstance.AppKey.Name)
|
|
data.Description = types.StringValue("")
|
|
data.Config = types.StringValue("")
|
|
data.Status = types.StringValue(appInstance.State)
|
|
|
|
tflog.Trace(ctx, "read an app instance data source")
|
|
|
|
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
|
}
|