Update appinstance data schema to return list

This commit is contained in:
Martin McCaffery 2025-11-13 12:12:34 +01:00
parent 39b88941a6
commit 676370b62b
Signed by: martin.mccaffery
GPG key ID: 7C4D0F375BCEE533
3 changed files with 78 additions and 19 deletions

View file

@ -8,12 +8,15 @@ terraform {
provider "edge-connect" {
endpoint = "https://hub.apps.edge.platform.mg3.mdb.osc.live"
variable "app_organization" {
default = "edp2"
}
resource "edge-connect_app" "edge_app_demo" {
name = "edge-app-demo"
app_version = "1.0.0"
organization = "edp2"
organization = var.app_organization
manifest = file("${path.module}/k8s-deployment.yaml")
@ -41,7 +44,7 @@ resource "edge-connect_app" "edge_app_demo" {
resource "edge-connect_app_instance" "edge_app_demo" {
name = "edge-app-demo-instance"
organization = "edp2"
organization = var.app_organization
region = "EU"
cloudlet_org = "TelekomOP"
cloudlet_name = "Munich"
@ -51,6 +54,15 @@ resource "edge-connect_app_instance" "edge_app_demo" {
flavor_name = "EU.small"
}
data "edge-connect_app_instance" "edge_app_demo" {
region = edge-connect_app_instance.edge_app_demo.region
}
output "app_data_retrieved" {
description = "Indicates if app instance data was retrieved"
value = data.edge-connect_app_instance.edge_app_demo
}
output "app_id" {
description = "ID of the EdgeConnect app"
value = edge-connect_app.edge_app_demo.id

View file

@ -31,30 +31,39 @@ type AppInstanceDataSourceModel struct {
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: "AppInstance data source",
MarkdownDescription: "AppInstances data source",
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
MarkdownDescription: "AppInstance name (primary key)",
Required: true,
Optional: true,
},
"organization": schema.StringAttribute{
MarkdownDescription: "Organization name (primary key)",
Required: true,
Optional: true,
},
"cloudlet_name": schema.StringAttribute{
MarkdownDescription: "Cloudlet name (primary key)",
Required: true,
Optional: true,
},
"cloudlet_organization": schema.StringAttribute{
MarkdownDescription: "Cloudlet organization (primary key)",
Required: true,
Optional: true,
},
"region": schema.StringAttribute{
MarkdownDescription: "Region (EU or US) (primary key)",
@ -62,7 +71,38 @@ func (d *AppInstanceDataSource) Schema(ctx context.Context, req datasource.Schem
},
"app_id": schema.StringAttribute{
MarkdownDescription: "Associated App ID",
Computed: true,
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,
},
},
},
},
},
}
@ -88,7 +128,7 @@ func (d *AppInstanceDataSource) Configure(ctx context.Context, req datasource.Co
}
func (d *AppInstanceDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data AppInstanceDataSourceModel
var data AppInstancesDataSourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
@ -107,17 +147,24 @@ func (d *AppInstanceDataSource) Read(ctx context.Context, req datasource.ReadReq
region := data.Region.ValueString()
appInstance, err := d.client.ShowAppInstance(ctx, appInstKey, region)
appInstances, err := d.client.ShowAppInstances(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")
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)...)
}

View file

@ -152,13 +152,13 @@ func (p *EdgeConnectProvider) Configure(ctx context.Context, req provider.Config
var client *edgeclient.Client
if token != "" {
client = edgeclient.NewClient(endpoint,
edgeclient.WithHTTPClient(&http.Client{Timeout: 30*time.Second}),
edgeclient.WithHTTPClient(&http.Client{Timeout: 30 * time.Second}),
edgeclient.WithAuthProvider(edgeclient.NewStaticTokenProvider(token)),
edgeclient.WithLogger(tfLogger{}),
)
} else {
client = edgeclient.NewClientWithCredentials(endpoint, username, password,
edgeclient.WithHTTPClient(&http.Client{Timeout: 30*time.Second}),
edgeclient.WithHTTPClient(&http.Client{Timeout: 30 * time.Second}),
edgeclient.WithLogger(tfLogger{}),
)
}
@ -194,8 +194,8 @@ func New(version string) func() provider.Provider {
}
}
type tfLogger struct {}
type tfLogger struct{}
func (tfLogger) Printf(format string, v ...interface{}){
func (tfLogger) Printf(format string, v ...interface{}) {
tflog.Debug(context.TODO(), fmt.Sprintf(format, v...))
}