248 lines
7.3 KiB
Go
248 lines
7.3 KiB
Go
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"
|
|
|
|
edgeclient "edp.buildth.ing/DevFW-CICD/edge-connect-client/v2/sdk/edgeconnect"
|
|
)
|
|
|
|
var _ resource.Resource = &AppInstanceResource{}
|
|
var _ resource.ResourceWithImportState = &AppInstanceResource{}
|
|
|
|
func NewAppInstanceResource() resource.Resource {
|
|
return &AppInstanceResource{}
|
|
}
|
|
|
|
type AppInstanceResource struct {
|
|
client *edgeclient.Client
|
|
}
|
|
|
|
type AppInstanceResourceModel 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 (r *AppInstanceResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
|
|
resp.TypeName = req.ProviderTypeName + "_app_instance"
|
|
}
|
|
|
|
func (r *AppInstanceResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
|
|
resp.Schema = schema.Schema{
|
|
MarkdownDescription: "AppInstance resource",
|
|
|
|
Attributes: map[string]schema.Attribute{
|
|
"id": schema.StringAttribute{
|
|
Computed: true,
|
|
MarkdownDescription: "AppInstance identifier",
|
|
PlanModifiers: []planmodifier.String{
|
|
stringplanmodifier.UseStateForUnknown(),
|
|
},
|
|
},
|
|
"name": schema.StringAttribute{
|
|
MarkdownDescription: "AppInstance name",
|
|
Required: true,
|
|
},
|
|
"app_id": schema.StringAttribute{
|
|
MarkdownDescription: "Associated App ID",
|
|
Required: true,
|
|
},
|
|
"description": schema.StringAttribute{
|
|
MarkdownDescription: "AppInstance description",
|
|
Optional: true,
|
|
},
|
|
"config": schema.StringAttribute{
|
|
MarkdownDescription: "AppInstance configuration",
|
|
Optional: true,
|
|
},
|
|
"status": schema.StringAttribute{
|
|
MarkdownDescription: "AppInstance status",
|
|
Computed: true,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func (r *AppInstanceResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
|
|
if req.ProviderData == nil {
|
|
return
|
|
}
|
|
|
|
client, ok := req.ProviderData.(*edgeclient.Client)
|
|
|
|
if !ok {
|
|
resp.Diagnostics.AddError(
|
|
"Unexpected Resource Configure Type",
|
|
fmt.Sprintf("Expected *edgeclient.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
|
|
)
|
|
|
|
return
|
|
}
|
|
|
|
r.client = client
|
|
}
|
|
|
|
func (r *AppInstanceResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
|
|
var data AppInstanceResourceModel
|
|
|
|
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
|
|
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
appInstInput := &edgeclient.NewAppInstanceInput{
|
|
Region: "default",
|
|
AppInst: edgeclient.AppInstance{
|
|
Key: edgeclient.AppInstanceKey{
|
|
Organization: "default",
|
|
Name: data.Name.ValueString(),
|
|
CloudletKey: edgeclient.CloudletKey{
|
|
Organization: "default",
|
|
Name: "default-cloudlet",
|
|
},
|
|
},
|
|
AppKey: edgeclient.AppKey{
|
|
Organization: "default",
|
|
Name: data.AppId.ValueString(),
|
|
Version: "1.0",
|
|
},
|
|
Flavor: edgeclient.Flavor{Name: "m4.small"},
|
|
},
|
|
}
|
|
|
|
err := r.client.CreateAppInstance(ctx, appInstInput)
|
|
if err != nil {
|
|
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create app instance, got error: %s", err))
|
|
return
|
|
}
|
|
|
|
data.Id = types.StringValue(appInstInput.AppInst.Key.Name)
|
|
data.Name = types.StringValue(appInstInput.AppInst.Key.Name)
|
|
data.AppId = types.StringValue(appInstInput.AppInst.AppKey.Name)
|
|
data.Description = types.StringValue("")
|
|
data.Config = types.StringValue("")
|
|
data.Status = types.StringValue("created")
|
|
|
|
tflog.Trace(ctx, "created an app instance resource")
|
|
|
|
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
|
}
|
|
|
|
func (r *AppInstanceResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
|
|
var data AppInstanceResourceModel
|
|
|
|
resp.Diagnostics.Append(req.State.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 := r.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)
|
|
|
|
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
|
}
|
|
|
|
func (r *AppInstanceResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
|
|
var data AppInstanceResourceModel
|
|
|
|
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
|
|
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
updateInput := &edgeclient.UpdateAppInstanceInput{
|
|
Region: "default",
|
|
AppInst: edgeclient.AppInstance{
|
|
Key: edgeclient.AppInstanceKey{
|
|
Organization: "default",
|
|
Name: data.Name.ValueString(),
|
|
CloudletKey: edgeclient.CloudletKey{
|
|
Organization: "default",
|
|
Name: "default-cloudlet",
|
|
},
|
|
},
|
|
AppKey: edgeclient.AppKey{
|
|
Organization: "default",
|
|
Name: data.AppId.ValueString(),
|
|
Version: "1.0",
|
|
},
|
|
Flavor: edgeclient.Flavor{Name: "m4.small"},
|
|
},
|
|
}
|
|
|
|
err := r.client.UpdateAppInstance(ctx, updateInput)
|
|
if err != nil {
|
|
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to update app instance, got error: %s", err))
|
|
return
|
|
}
|
|
|
|
data.Name = types.StringValue(updateInput.AppInst.Key.Name)
|
|
data.AppId = types.StringValue(updateInput.AppInst.AppKey.Name)
|
|
data.Description = types.StringValue("")
|
|
data.Config = types.StringValue("")
|
|
data.Status = types.StringValue("updated")
|
|
|
|
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
|
}
|
|
|
|
func (r *AppInstanceResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
|
|
var data AppInstanceResourceModel
|
|
|
|
resp.Diagnostics.Append(req.State.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",
|
|
},
|
|
}
|
|
|
|
err := r.client.DeleteAppInstance(ctx, appInstKey, "default")
|
|
if err != nil {
|
|
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete app instance, got error: %s", err))
|
|
return
|
|
}
|
|
}
|
|
|
|
func (r *AppInstanceResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
|
|
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
|
|
}
|