361 lines
11 KiB
Go
361 lines
11 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"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/int64planmodifier"
|
|
"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/v2"
|
|
)
|
|
|
|
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"`
|
|
AppVersion types.String `tfsdk:"app_version"`
|
|
Organization types.String `tfsdk:"organization"`
|
|
Manifest types.String `tfsdk:"manifest"`
|
|
Region types.String `tfsdk:"region"`
|
|
CloudletOrg types.String `tfsdk:"cloudlet_org"`
|
|
CloudletName types.String `tfsdk:"cloudlet_name"`
|
|
FlavorName types.String `tfsdk:"flavor_name"`
|
|
Network *NetworkModel `tfsdk:"network"`
|
|
}
|
|
|
|
type NetworkModel struct {
|
|
OutboundConnections []OutboundConnectionModel `tfsdk:"outbound_connections"`
|
|
}
|
|
|
|
type OutboundConnectionModel struct {
|
|
Protocol types.String `tfsdk:"protocol"`
|
|
PortRangeMin types.Int64 `tfsdk:"port_range_min"`
|
|
PortRangeMax types.Int64 `tfsdk:"port_range_max"`
|
|
RemoteCIDR types.String `tfsdk:"remote_cidr"`
|
|
}
|
|
|
|
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: "EdgeConnect App deployment 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,
|
|
PlanModifiers: []planmodifier.String{
|
|
stringplanmodifier.RequiresReplace(),
|
|
},
|
|
},
|
|
"app_version": schema.StringAttribute{
|
|
MarkdownDescription: "App version",
|
|
Required: true,
|
|
PlanModifiers: []planmodifier.String{
|
|
stringplanmodifier.RequiresReplace(),
|
|
},
|
|
},
|
|
"organization": schema.StringAttribute{
|
|
MarkdownDescription: "Organization name",
|
|
Required: true,
|
|
PlanModifiers: []planmodifier.String{
|
|
stringplanmodifier.RequiresReplace(),
|
|
},
|
|
},
|
|
"manifest": schema.StringAttribute{
|
|
MarkdownDescription: "Kubernetes manifest YAML content",
|
|
Required: true,
|
|
PlanModifiers: []planmodifier.String{
|
|
stringplanmodifier.RequiresReplace(),
|
|
},
|
|
},
|
|
"region": schema.StringAttribute{
|
|
MarkdownDescription: "Region (e.g., US, EU)",
|
|
Required: true,
|
|
PlanModifiers: []planmodifier.String{
|
|
stringplanmodifier.RequiresReplace(),
|
|
},
|
|
},
|
|
"cloudlet_org": schema.StringAttribute{
|
|
MarkdownDescription: "Cloudlet organization",
|
|
Required: true,
|
|
PlanModifiers: []planmodifier.String{
|
|
stringplanmodifier.RequiresReplace(),
|
|
},
|
|
},
|
|
"cloudlet_name": schema.StringAttribute{
|
|
MarkdownDescription: "Cloudlet name",
|
|
Required: true,
|
|
PlanModifiers: []planmodifier.String{
|
|
stringplanmodifier.RequiresReplace(),
|
|
},
|
|
},
|
|
"flavor_name": schema.StringAttribute{
|
|
MarkdownDescription: "Flavor name",
|
|
Required: true,
|
|
PlanModifiers: []planmodifier.String{
|
|
stringplanmodifier.RequiresReplace(),
|
|
},
|
|
},
|
|
},
|
|
Blocks: map[string]schema.Block{
|
|
"network": schema.SingleNestedBlock{
|
|
MarkdownDescription: "Network configuration",
|
|
Blocks: map[string]schema.Block{
|
|
"outbound_connections": schema.ListNestedBlock{
|
|
MarkdownDescription: "Outbound connection rules",
|
|
NestedObject: schema.NestedBlockObject{
|
|
Attributes: map[string]schema.Attribute{
|
|
"protocol": schema.StringAttribute{
|
|
MarkdownDescription: "Protocol (tcp, udp, icmp)",
|
|
Required: true,
|
|
PlanModifiers: []planmodifier.String{
|
|
stringplanmodifier.RequiresReplace(),
|
|
},
|
|
},
|
|
"port_range_min": schema.Int64Attribute{
|
|
MarkdownDescription: "Minimum port number",
|
|
Required: true,
|
|
PlanModifiers: []planmodifier.Int64{
|
|
int64planmodifier.RequiresReplace(),
|
|
},
|
|
},
|
|
"port_range_max": schema.Int64Attribute{
|
|
MarkdownDescription: "Maximum port number",
|
|
Required: true,
|
|
PlanModifiers: []planmodifier.Int64{
|
|
int64planmodifier.RequiresReplace(),
|
|
},
|
|
},
|
|
"remote_cidr": schema.StringAttribute{
|
|
MarkdownDescription: "Remote CIDR (e.g., 0.0.0.0/0)",
|
|
Required: true,
|
|
PlanModifiers: []planmodifier.String{
|
|
stringplanmodifier.RequiresReplace(),
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
// Build outbound connections from network
|
|
var outboundConnections []edgeclient.SecurityRule
|
|
if data.Network != nil && len(data.Network.OutboundConnections) > 0 {
|
|
for _, conn := range data.Network.OutboundConnections {
|
|
outboundConnections = append(outboundConnections, edgeclient.SecurityRule{
|
|
Protocol: conn.Protocol.ValueString(),
|
|
PortRangeMin: int(conn.PortRangeMin.ValueInt64()),
|
|
PortRangeMax: int(conn.PortRangeMax.ValueInt64()),
|
|
RemoteCIDR: conn.RemoteCIDR.ValueString(),
|
|
})
|
|
}
|
|
}
|
|
|
|
appInput := &edgeclient.NewAppInput{
|
|
Region: data.Region.ValueString(),
|
|
App: edgeclient.App{
|
|
Key: edgeclient.AppKey{
|
|
Organization: data.Organization.ValueString(),
|
|
Name: data.Name.ValueString(),
|
|
Version: data.AppVersion.ValueString(),
|
|
},
|
|
DefaultFlavor: edgeclient.Flavor{
|
|
Name: data.FlavorName.ValueString(),
|
|
},
|
|
AllowServerless: true,
|
|
Deployment: "kubernetes",
|
|
ServerlessConfig: struct{}{},
|
|
ImageType: "Docker",
|
|
DeploymentGenerator: "kubernetes-basic",
|
|
ImagePath: "docker.io/library/nginx:latest",
|
|
DeploymentManifest: data.Manifest.ValueString(),
|
|
RequiredOutboundConnections: outboundConnections,
|
|
},
|
|
}
|
|
|
|
appInputJson, _ := json.Marshal(appInput)
|
|
tflog.Info(ctx, fmt.Sprintf("appInput: %v\n", string(appInputJson)), map[string]interface{}{})
|
|
|
|
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)
|
|
|
|
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: data.Organization.ValueString(),
|
|
Name: data.Id.ValueString(),
|
|
Version: data.AppVersion.ValueString(),
|
|
}
|
|
|
|
app, err := r.client.ShowApp(ctx, appKey, data.Region.ValueString())
|
|
if err != nil {
|
|
|
|
if errors.Is(err, edgeclient.ErrResourceNotFound) {
|
|
resp.State.RemoveResource(ctx)
|
|
return
|
|
}
|
|
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read app %s, got error: %s", data.Id.ValueString(), err))
|
|
return
|
|
}
|
|
|
|
// Update state from API response
|
|
data.Name = types.StringValue(app.Key.Name)
|
|
data.AppVersion = types.StringValue(app.Key.Version)
|
|
data.Organization = types.StringValue(app.Key.Organization)
|
|
data.Manifest = types.StringValue(app.DeploymentManifest)
|
|
|
|
// Note: The API returns RequiredOutboundConnections but not the full infrastructure details
|
|
// We preserve the existing region, cloudlet, flavor, and network from state since the API doesn't return them
|
|
|
|
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
|
|
}
|
|
|
|
// Build outbound connections from network
|
|
var outboundConnections []edgeclient.SecurityRule
|
|
if data.Network != nil && len(data.Network.OutboundConnections) > 0 {
|
|
for _, conn := range data.Network.OutboundConnections {
|
|
outboundConnections = append(outboundConnections, edgeclient.SecurityRule{
|
|
Protocol: conn.Protocol.ValueString(),
|
|
PortRangeMin: int(conn.PortRangeMin.ValueInt64()),
|
|
PortRangeMax: int(conn.PortRangeMax.ValueInt64()),
|
|
RemoteCIDR: conn.RemoteCIDR.ValueString(),
|
|
})
|
|
}
|
|
}
|
|
|
|
updateInput := &edgeclient.UpdateAppInput{
|
|
Region: data.Region.ValueString(),
|
|
App: edgeclient.App{
|
|
Key: edgeclient.AppKey{
|
|
Organization: data.Organization.ValueString(),
|
|
Name: data.Name.ValueString(),
|
|
Version: data.AppVersion.ValueString(),
|
|
},
|
|
Deployment: "kubernetes",
|
|
ImageType: "docker",
|
|
DeploymentManifest: data.Manifest.ValueString(),
|
|
RequiredOutboundConnections: outboundConnections,
|
|
},
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
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: data.Organization.ValueString(),
|
|
Name: data.Id.ValueString(),
|
|
Version: data.AppVersion.ValueString(),
|
|
}
|
|
|
|
err := r.client.DeleteApp(ctx, appKey, data.Region.ValueString())
|
|
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)
|
|
}
|