204 lines
7.2 KiB
Go
204 lines
7.2 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/DevFW-CICD/terraform-provider-edge-connect/internal/client"
|
|
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
|
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
|
|
"github.com/hashicorp/terraform-plugin-framework/types"
|
|
)
|
|
|
|
// Ensure the implementation satisfies the expected interfaces.
|
|
var (
|
|
_ datasource.DataSource = &appInstDataSource{}
|
|
_ datasource.DataSourceWithConfigure = &appInstDataSource{}
|
|
)
|
|
|
|
// NewAppInstDataSource is a helper function to simplify the provider implementation.
|
|
func NewAppInstDataSource() datasource.DataSource {
|
|
return &appInstDataSource{}
|
|
}
|
|
|
|
// appInstDataSource is the data source implementation.
|
|
type appInstDataSource struct {
|
|
client *client.Client
|
|
}
|
|
|
|
// appInstDataSourceModel maps the data source schema data.
|
|
type appInstDataSourceModel struct {
|
|
ID types.String `tfsdk:"id"`
|
|
Region types.String `tfsdk:"region"`
|
|
AppOrganization types.String `tfsdk:"app_organization"`
|
|
AppName types.String `tfsdk:"app_name"`
|
|
AppVersion types.String `tfsdk:"app_version"`
|
|
CloudletOrganization types.String `tfsdk:"cloudlet_organization"`
|
|
CloudletName types.String `tfsdk:"cloudlet_name"`
|
|
ClusterOrganization types.String `tfsdk:"cluster_organization"`
|
|
Cloudlet types.String `tfsdk:"cloudlet"`
|
|
Flavor types.String `tfsdk:"flavor"`
|
|
RealClusterName types.String `tfsdk:"real_cluster_name"`
|
|
State types.String `tfsdk:"state"`
|
|
RuntimeInfo types.String `tfsdk:"runtime_info"`
|
|
Uri types.String `tfsdk:"uri"`
|
|
Liveness types.String `tfsdk:"liveness"`
|
|
PowerState types.String `tfsdk:"power_state"`
|
|
CreatedAt types.String `tfsdk:"created_at"`
|
|
UpdatedAt types.String `tfsdk:"updated_at"`
|
|
}
|
|
|
|
// Metadata returns the data source type name.
|
|
func (d *appInstDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
|
|
resp.TypeName = req.ProviderTypeName + "_appinst"
|
|
}
|
|
|
|
// Schema defines the schema for the data source.
|
|
func (d *appInstDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
|
|
resp.Schema = schema.Schema{
|
|
Description: "Fetches an Edge Connect application instance.",
|
|
Attributes: map[string]schema.Attribute{
|
|
"id": schema.StringAttribute{
|
|
Description: "The unique identifier for the app instance.",
|
|
Computed: true,
|
|
},
|
|
"region": schema.StringAttribute{
|
|
Description: "The region where the app instance is deployed (e.g., 'EU').",
|
|
Required: true,
|
|
},
|
|
"app_organization": schema.StringAttribute{
|
|
Description: "The organization that owns the app.",
|
|
Required: true,
|
|
},
|
|
"app_name": schema.StringAttribute{
|
|
Description: "The name of the application.",
|
|
Required: true,
|
|
},
|
|
"app_version": schema.StringAttribute{
|
|
Description: "The version of the application.",
|
|
Required: true,
|
|
},
|
|
"cloudlet_organization": schema.StringAttribute{
|
|
Description: "The organization that owns the cloudlet.",
|
|
Required: true,
|
|
},
|
|
"cloudlet_name": schema.StringAttribute{
|
|
Description: "The name of the cloudlet.",
|
|
Required: true,
|
|
},
|
|
"cluster_organization": schema.StringAttribute{
|
|
Description: "The organization that owns the cluster.",
|
|
Required: true,
|
|
},
|
|
"cloudlet": schema.StringAttribute{
|
|
Description: "The cloudlet identifier.",
|
|
Computed: true,
|
|
},
|
|
"flavor": schema.StringAttribute{
|
|
Description: "The flavor for the app instance.",
|
|
Computed: true,
|
|
},
|
|
"real_cluster_name": schema.StringAttribute{
|
|
Description: "The real cluster name.",
|
|
Computed: true,
|
|
},
|
|
"state": schema.StringAttribute{
|
|
Description: "The state of the app instance.",
|
|
Computed: true,
|
|
},
|
|
"runtime_info": schema.StringAttribute{
|
|
Description: "Runtime information for the app instance.",
|
|
Computed: true,
|
|
},
|
|
"uri": schema.StringAttribute{
|
|
Description: "The URI to access the app instance.",
|
|
Computed: true,
|
|
},
|
|
"liveness": schema.StringAttribute{
|
|
Description: "The liveness status of the app instance.",
|
|
Computed: true,
|
|
},
|
|
"power_state": schema.StringAttribute{
|
|
Description: "The power state of the app instance.",
|
|
Computed: true,
|
|
},
|
|
"created_at": schema.StringAttribute{
|
|
Description: "The timestamp when the app instance was created.",
|
|
Computed: true,
|
|
},
|
|
"updated_at": schema.StringAttribute{
|
|
Description: "The timestamp when the app instance was last updated.",
|
|
Computed: true,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
// Configure adds the provider configured client to the data source.
|
|
func (d *appInstDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
|
|
if req.ProviderData == nil {
|
|
return
|
|
}
|
|
|
|
client, ok := req.ProviderData.(*client.Client)
|
|
if !ok {
|
|
resp.Diagnostics.AddError(
|
|
"Unexpected Data Source Configure Type",
|
|
fmt.Sprintf("Expected *client.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
|
|
)
|
|
return
|
|
}
|
|
|
|
d.client = client
|
|
}
|
|
|
|
// Read refreshes the Terraform state with the latest data.
|
|
func (d *appInstDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
|
|
var config appInstDataSourceModel
|
|
diags := req.Config.Get(ctx, &config)
|
|
resp.Diagnostics.Append(diags...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
// Get app instance from API
|
|
appInst := client.AppInst{}
|
|
appInst.Key.AppKey.Organization = config.AppOrganization.ValueString()
|
|
appInst.Key.AppKey.Name = config.AppName.ValueString()
|
|
appInst.Key.AppKey.Version = config.AppVersion.ValueString()
|
|
appInst.Key.ClusterInstKey.CloudletKey.Organization = config.CloudletOrganization.ValueString()
|
|
appInst.Key.ClusterInstKey.CloudletKey.Name = config.CloudletName.ValueString()
|
|
appInst.Key.ClusterInstKey.Organization = config.ClusterOrganization.ValueString()
|
|
|
|
readAppInst, err := d.client.GetAppInst(config.Region.ValueString(), appInst)
|
|
if err != nil {
|
|
resp.Diagnostics.AddError(
|
|
"Error reading app instance",
|
|
"Could not read app instance: "+err.Error(),
|
|
)
|
|
return
|
|
}
|
|
|
|
// Map response to data source model
|
|
config.ID = types.StringValue(fmt.Sprintf("%s/%s/%s/%s/%s/%s/%s",
|
|
config.Region.ValueString(),
|
|
config.AppOrganization.ValueString(),
|
|
config.AppName.ValueString(),
|
|
config.AppVersion.ValueString(),
|
|
config.CloudletOrganization.ValueString(),
|
|
config.CloudletName.ValueString(),
|
|
config.ClusterOrganization.ValueString()))
|
|
config.Cloudlet = types.StringValue(readAppInst.Cloudlet)
|
|
config.Flavor = types.StringValue(readAppInst.Flavor)
|
|
config.RealClusterName = types.StringValue(readAppInst.RealClusterName)
|
|
config.State = types.StringValue(readAppInst.State)
|
|
config.RuntimeInfo = types.StringValue(readAppInst.RuntimeInfo)
|
|
config.Uri = types.StringValue(readAppInst.Uri)
|
|
config.Liveness = types.StringValue(readAppInst.Liveness)
|
|
config.PowerState = types.StringValue(readAppInst.PowerState)
|
|
config.CreatedAt = types.StringValue(readAppInst.CreatedAt)
|
|
config.UpdatedAt = types.StringValue(readAppInst.UpdatedAt)
|
|
|
|
diags = resp.State.Set(ctx, &config)
|
|
resp.Diagnostics.Append(diags...)
|
|
}
|