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

331 lines
11 KiB
Go
Raw Normal View History

2025-11-11 14:15:52 +01:00
package provider
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
2025-11-11 15:25:57 +01:00
edgeclient "edp.buildth.ing/DevFW-CICD/edge-connect-client/v2/sdk/edgeconnect"
2025-11-11 14:15:52 +01:00
)
2025-11-11 15:25:57 +01:00
var _ resource.Resource = &AppInstanceResource{}
var _ resource.ResourceWithImportState = &AppInstanceResource{}
func NewAppInstanceResource() resource.Resource {
return &AppInstanceResource{}
2025-11-11 14:15:52 +01:00
}
2025-11-11 15:25:57 +01:00
type AppInstanceResource struct {
client *edgeclient.Client
2025-11-11 14:15:52 +01:00
}
2025-11-11 15:25:57 +01:00
type AppInstanceResourceModel struct {
2025-11-11 17:20:04 +01:00
Id types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Organization types.String `tfsdk:"organization"`
Region types.String `tfsdk:"region"`
CloudletOrg types.String `tfsdk:"cloudlet_org"`
CloudletName types.String `tfsdk:"cloudlet_name"`
AppName types.String `tfsdk:"app_name"`
AppVersion types.String `tfsdk:"app_version"`
AppOrganization types.String `tfsdk:"app_organization"`
FlavorName types.String `tfsdk:"flavor_name"`
State types.String `tfsdk:"state"`
PowerState types.String `tfsdk:"power_state"`
2025-11-11 14:15:52 +01:00
}
2025-11-11 15:25:57 +01:00
func (r *AppInstanceResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_app_instance"
2025-11-11 14:15:52 +01:00
}
2025-11-11 15:25:57 +01:00
func (r *AppInstanceResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
2025-11-11 14:15:52 +01:00
resp.Schema = schema.Schema{
2025-11-11 17:20:04 +01:00
MarkdownDescription: "EdgeConnect AppInstance deployment resource",
2025-11-11 15:25:57 +01:00
2025-11-11 14:15:52 +01:00
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
2025-11-11 15:25:57 +01:00
Computed: true,
MarkdownDescription: "AppInstance identifier",
2025-11-11 14:15:52 +01:00
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
2025-11-11 15:25:57 +01:00
"name": schema.StringAttribute{
MarkdownDescription: "AppInstance name",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
2025-11-11 14:15:52 +01:00
},
2025-11-11 17:20:04 +01:00
"organization": schema.StringAttribute{
MarkdownDescription: "Organization name",
2025-11-11 15:25:57 +01:00
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
2025-11-11 14:15:52 +01:00
},
2025-11-11 17:20:04 +01:00
"region": schema.StringAttribute{
MarkdownDescription: "Region (e.g., US, EU)",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
2025-11-11 17:20:04 +01:00
},
"cloudlet_org": schema.StringAttribute{
MarkdownDescription: "Cloudlet organization",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
2025-11-11 17:20:04 +01:00
},
"cloudlet_name": schema.StringAttribute{
MarkdownDescription: "Cloudlet name",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
2025-11-11 17:20:04 +01:00
},
"app_name": schema.StringAttribute{
MarkdownDescription: "Application name",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
2025-11-11 14:15:52 +01:00
},
2025-11-11 17:20:04 +01:00
"app_version": schema.StringAttribute{
MarkdownDescription: "Application version",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
2025-11-11 17:20:04 +01:00
},
"app_organization": schema.StringAttribute{
MarkdownDescription: "Application organization",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
2025-11-11 17:20:04 +01:00
},
"flavor_name": schema.StringAttribute{
MarkdownDescription: "Flavor name",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
2025-11-11 17:20:04 +01:00
},
"state": schema.StringAttribute{
MarkdownDescription: "AppInstance state",
Computed: true,
2025-11-11 14:15:52 +01:00
},
2025-11-11 17:20:04 +01:00
"power_state": schema.StringAttribute{
MarkdownDescription: "AppInstance power state",
2025-11-11 15:25:57 +01:00
Computed: true,
2025-11-11 14:15:52 +01:00
},
},
}
}
2025-11-11 15:25:57 +01:00
func (r *AppInstanceResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
2025-11-11 14:15:52 +01:00
if req.ProviderData == nil {
return
}
2025-11-11 15:25:57 +01:00
client, ok := req.ProviderData.(*edgeclient.Client)
2025-11-11 14:15:52 +01:00
if !ok {
resp.Diagnostics.AddError(
"Unexpected Resource Configure Type",
2025-11-11 15:25:57 +01:00
fmt.Sprintf("Expected *edgeclient.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
2025-11-11 14:15:52 +01:00
)
2025-11-11 15:25:57 +01:00
2025-11-11 14:15:52 +01:00
return
}
r.client = client
}
2025-11-11 15:25:57 +01:00
func (r *AppInstanceResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data AppInstanceResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
2025-11-11 14:15:52 +01:00
if resp.Diagnostics.HasError() {
return
}
2025-11-11 15:25:57 +01:00
appInstInput := &edgeclient.NewAppInstanceInput{
2025-11-11 17:20:04 +01:00
Region: data.Region.ValueString(),
2025-11-11 15:25:57 +01:00
AppInst: edgeclient.AppInstance{
Key: edgeclient.AppInstanceKey{
2025-11-11 17:20:04 +01:00
Organization: data.Organization.ValueString(),
2025-11-11 15:25:57 +01:00
Name: data.Name.ValueString(),
CloudletKey: edgeclient.CloudletKey{
2025-11-11 17:20:04 +01:00
Organization: data.CloudletOrg.ValueString(),
Name: data.CloudletName.ValueString(),
2025-11-11 15:25:57 +01:00
},
},
AppKey: edgeclient.AppKey{
2025-11-11 17:20:04 +01:00
Organization: data.AppOrganization.ValueString(),
Name: data.AppName.ValueString(),
Version: data.AppVersion.ValueString(),
2025-11-11 15:25:57 +01:00
},
2025-11-11 17:20:04 +01:00
Flavor: edgeclient.Flavor{Name: data.FlavorName.ValueString()},
2025-11-11 15:25:57 +01:00
},
2025-11-11 14:15:52 +01:00
}
2025-11-11 15:25:57 +01:00
err := r.client.CreateAppInstance(ctx, appInstInput)
2025-11-11 14:15:52 +01:00
if err != nil {
2025-11-11 15:25:57 +01:00
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create app instance, got error: %s", err))
2025-11-11 14:15:52 +01:00
return
}
2025-11-11 15:25:57 +01:00
data.Id = types.StringValue(appInstInput.AppInst.Key.Name)
2025-11-11 17:20:04 +01:00
data.State = types.StringValue("created")
2025-11-11 15:25:57 +01:00
appInstance, err := r.client.ShowAppInstance(ctx, appInstInput.AppInst.Key, appInstInput.AppInst.AppKey, data.Region.ValueString())
2025-11-12 11:35:02 +01:00
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read app instance %s, got error: %s", data.Id.ValueString(), err))
return
}
// Update state from API response
UpdateDataFromAppInstance(&appInstance, &data)
2025-11-11 15:25:57 +01:00
tflog.Trace(ctx, "created an app instance resource")
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
2025-11-11 14:15:52 +01:00
}
2025-11-11 15:25:57 +01:00
func (r *AppInstanceResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data AppInstanceResourceModel
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
2025-11-11 14:15:52 +01:00
if resp.Diagnostics.HasError() {
return
}
2025-11-11 15:25:57 +01:00
appInstKey := edgeclient.AppInstanceKey{
2025-11-11 17:20:04 +01:00
Organization: data.Organization.ValueString(),
2025-11-11 15:25:57 +01:00
Name: data.Id.ValueString(),
CloudletKey: edgeclient.CloudletKey{
2025-11-11 17:20:04 +01:00
Organization: data.CloudletOrg.ValueString(),
Name: data.CloudletName.ValueString(),
2025-11-11 15:25:57 +01:00
},
}
appKey := edgeclient.AppKey{
Name: data.AppName.ValueString(),
}
2025-11-11 14:15:52 +01:00
appInstance, err := r.client.ShowAppInstance(ctx, appInstKey, appKey, data.Region.ValueString())
2025-11-11 14:15:52 +01:00
if err != nil {
2025-11-11 15:25:57 +01:00
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read app instance %s, got error: %s", data.Id.ValueString(), err))
2025-11-11 14:15:52 +01:00
return
}
2025-11-11 17:20:04 +01:00
// Update state from API response
2025-11-12 11:35:02 +01:00
UpdateDataFromAppInstance(&appInstance, &data)
2025-11-11 15:25:57 +01:00
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
2025-11-11 14:15:52 +01:00
}
2025-11-11 15:25:57 +01:00
func (r *AppInstanceResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var data AppInstanceResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
2025-11-11 14:15:52 +01:00
if resp.Diagnostics.HasError() {
return
}
2025-11-11 15:25:57 +01:00
updateInput := &edgeclient.UpdateAppInstanceInput{
2025-11-11 17:20:04 +01:00
Region: data.Region.ValueString(),
2025-11-11 15:25:57 +01:00
AppInst: edgeclient.AppInstance{
Key: edgeclient.AppInstanceKey{
2025-11-11 17:20:04 +01:00
Organization: data.Organization.ValueString(),
2025-11-11 15:25:57 +01:00
Name: data.Name.ValueString(),
CloudletKey: edgeclient.CloudletKey{
2025-11-11 17:20:04 +01:00
Organization: data.CloudletOrg.ValueString(),
Name: data.CloudletName.ValueString(),
2025-11-11 15:25:57 +01:00
},
},
AppKey: edgeclient.AppKey{
2025-11-11 17:20:04 +01:00
Organization: data.AppOrganization.ValueString(),
Name: data.AppName.ValueString(),
Version: data.AppVersion.ValueString(),
2025-11-11 15:25:57 +01:00
},
2025-11-11 17:20:04 +01:00
Flavor: edgeclient.Flavor{Name: data.FlavorName.ValueString()},
2025-11-11 15:25:57 +01:00
},
2025-11-11 14:15:52 +01:00
}
appInput := edgeclient.AppKey{
Name: data.AppName.ValueString(),
}
2025-11-11 15:25:57 +01:00
err := r.client.UpdateAppInstance(ctx, updateInput)
2025-11-11 14:15:52 +01:00
if err != nil {
2025-11-11 15:25:57 +01:00
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to update app instance, got error: %s", err))
2025-11-11 14:15:52 +01:00
return
}
appInstance, err := r.client.ShowAppInstance(ctx, updateInput.AppInst.Key, appInput, data.Region.ValueString())
2025-11-12 11:35:02 +01:00
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read app instance %s, got error: %s", data.Id.ValueString(), err))
return
}
// Update state from API response
UpdateDataFromAppInstance(&appInstance, &data)
2025-11-11 17:20:04 +01:00
data.State = types.StringValue("updated")
2025-11-11 14:15:52 +01:00
2025-11-11 15:25:57 +01:00
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
2025-11-11 14:15:52 +01:00
}
2025-11-11 15:25:57 +01:00
func (r *AppInstanceResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var data AppInstanceResourceModel
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
2025-11-11 14:15:52 +01:00
if resp.Diagnostics.HasError() {
return
}
2025-11-11 15:25:57 +01:00
appInstKey := edgeclient.AppInstanceKey{
2025-11-11 17:20:04 +01:00
Organization: data.Organization.ValueString(),
2025-11-11 15:25:57 +01:00
Name: data.Id.ValueString(),
CloudletKey: edgeclient.CloudletKey{
2025-11-11 17:20:04 +01:00
Organization: data.CloudletOrg.ValueString(),
Name: data.CloudletName.ValueString(),
2025-11-11 15:25:57 +01:00
},
}
2025-11-11 14:15:52 +01:00
2025-11-11 17:20:04 +01:00
err := r.client.DeleteAppInstance(ctx, appInstKey, data.Region.ValueString())
2025-11-11 14:15:52 +01:00
if err != nil {
2025-11-11 15:25:57 +01:00
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete app instance, got error: %s", err))
2025-11-11 14:15:52 +01:00
return
}
}
2025-11-11 15:25:57 +01:00
func (r *AppInstanceResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
2025-11-11 14:15:52 +01:00
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}
2025-11-12 11:35:02 +01:00
func UpdateDataFromAppInstance(appInstance *edgeclient.AppInstance, data *AppInstanceResourceModel) {
2025-11-12 11:35:02 +01:00
data.Name = types.StringValue(appInstance.Key.Name)
data.Organization = types.StringValue(appInstance.Key.Organization)
data.CloudletOrg = types.StringValue(appInstance.Key.CloudletKey.Organization)
data.CloudletName = types.StringValue(appInstance.Key.CloudletKey.Name)
data.AppName = types.StringValue(appInstance.AppKey.Name)
data.AppVersion = types.StringValue(appInstance.AppKey.Version)
data.AppOrganization = types.StringValue(appInstance.AppKey.Organization)
data.FlavorName = types.StringValue(appInstance.Flavor.Name)
data.State = types.StringValue(appInstance.State)
data.PowerState = types.StringValue(appInstance.PowerState)
}