222 lines
6.2 KiB
Go
222 lines
6.2 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 = &AppResource{}
|
|
var _ resource.ResourceWithImportState = &AppResource{}
|
|
|
|
func NewAppResource() resource.Resource {
|
|
return &AppResource{}
|
|
}
|
|
|
|
type AppResource struct {
|
|
client *edgeclient.Client
|
|
}
|
|
|
|
type AppResourceModel struct {
|
|
Id types.String `tfsdk:"id"`
|
|
Name types.String `tfsdk:"name"`
|
|
Description types.String `tfsdk:"description"`
|
|
Version types.String `tfsdk:"version"`
|
|
Status types.String `tfsdk:"status"`
|
|
}
|
|
|
|
func (r *AppResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
|
|
resp.TypeName = req.ProviderTypeName + "_app"
|
|
}
|
|
|
|
func (r *AppResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
|
|
resp.Schema = schema.Schema{
|
|
MarkdownDescription: "App resource",
|
|
|
|
Attributes: map[string]schema.Attribute{
|
|
"id": schema.StringAttribute{
|
|
Computed: true,
|
|
MarkdownDescription: "App identifier",
|
|
PlanModifiers: []planmodifier.String{
|
|
stringplanmodifier.UseStateForUnknown(),
|
|
},
|
|
},
|
|
"name": schema.StringAttribute{
|
|
MarkdownDescription: "App name",
|
|
Required: true,
|
|
},
|
|
"description": schema.StringAttribute{
|
|
MarkdownDescription: "App description",
|
|
Optional: true,
|
|
},
|
|
"version": schema.StringAttribute{
|
|
MarkdownDescription: "App version",
|
|
Optional: true,
|
|
},
|
|
"status": schema.StringAttribute{
|
|
MarkdownDescription: "App status",
|
|
Computed: true,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func (r *AppResource) 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 *AppResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
|
|
var data AppResourceModel
|
|
|
|
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
|
|
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
appInput := &edgeclient.NewAppInput{
|
|
Region: "default",
|
|
App: edgeclient.App{
|
|
Key: edgeclient.AppKey{
|
|
Organization: "default",
|
|
Name: data.Name.ValueString(),
|
|
Version: data.Version.ValueString(),
|
|
},
|
|
Deployment: "kubernetes",
|
|
ImageType: "docker",
|
|
ImagePath: "nginx:latest",
|
|
},
|
|
}
|
|
|
|
err := r.client.CreateApp(ctx, appInput)
|
|
if err != nil {
|
|
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create app, got error: %s", err))
|
|
return
|
|
}
|
|
|
|
data.Id = types.StringValue(appInput.App.Key.Name)
|
|
data.Name = types.StringValue(appInput.App.Key.Name)
|
|
data.Description = types.StringValue("")
|
|
data.Version = types.StringValue(appInput.App.Key.Version)
|
|
data.Status = types.StringValue("created")
|
|
|
|
tflog.Trace(ctx, "created an app resource")
|
|
|
|
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
|
}
|
|
|
|
func (r *AppResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
|
|
var data AppResourceModel
|
|
|
|
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
|
|
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
appKey := edgeclient.AppKey{
|
|
Organization: "default",
|
|
Name: data.Id.ValueString(),
|
|
Version: data.Version.ValueString(),
|
|
}
|
|
|
|
app, err := r.client.ShowApp(ctx, appKey, "default")
|
|
if err != nil {
|
|
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read app %s, got error: %s", data.Id.ValueString(), err))
|
|
return
|
|
}
|
|
|
|
data.Name = types.StringValue(app.Key.Name)
|
|
data.Description = types.StringValue("")
|
|
data.Version = types.StringValue(app.Key.Version)
|
|
data.Status = types.StringValue("")
|
|
|
|
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
|
}
|
|
|
|
func (r *AppResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
|
|
var data AppResourceModel
|
|
|
|
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
|
|
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
updateInput := &edgeclient.UpdateAppInput{
|
|
Region: "default",
|
|
App: edgeclient.App{
|
|
Key: edgeclient.AppKey{
|
|
Organization: "default",
|
|
Name: data.Name.ValueString(),
|
|
Version: data.Version.ValueString(),
|
|
},
|
|
Deployment: "kubernetes",
|
|
ImageType: "docker",
|
|
ImagePath: "nginx:latest",
|
|
},
|
|
}
|
|
|
|
err := r.client.UpdateApp(ctx, updateInput)
|
|
if err != nil {
|
|
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to update app, got error: %s", err))
|
|
return
|
|
}
|
|
|
|
data.Name = types.StringValue(updateInput.App.Key.Name)
|
|
data.Description = types.StringValue("")
|
|
data.Version = types.StringValue(updateInput.App.Key.Version)
|
|
data.Status = types.StringValue("updated")
|
|
|
|
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
|
}
|
|
|
|
func (r *AppResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
|
|
var data AppResourceModel
|
|
|
|
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
|
|
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
appKey := edgeclient.AppKey{
|
|
Organization: "default",
|
|
Name: data.Id.ValueString(),
|
|
Version: data.Version.ValueString(),
|
|
}
|
|
|
|
err := r.client.DeleteApp(ctx, appKey, "default")
|
|
if err != nil {
|
|
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete app, got error: %s", err))
|
|
return
|
|
}
|
|
}
|
|
|
|
func (r *AppResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
|
|
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
|
|
}
|