Files
benvin 2e6bd1eff8
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
Add traffic-control provider resources (mangle/accounting/tc_*)
Batch 4 provider resources, id-keyed. Register and document.
2026-07-26 16:02:26 +10:00

152 lines
5.7 KiB
Go

package provider
import (
"context"
"strconv"
"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/types"
)
var (
_ resource.Resource = &tcDeviceResource{}
_ resource.ResourceWithImportState = &tcDeviceResource{}
)
type tcDeviceResource struct{ client *apiClient }
type tcDeviceModel struct {
ID types.Int64 `tfsdk:"id"`
Device types.String `tfsdk:"device"`
Interface types.String `tfsdk:"interface"`
InBandwidth types.String `tfsdk:"in_bandwidth"`
OutBandwidth types.String `tfsdk:"out_bandwidth"`
Comment types.String `tfsdk:"comment"`
}
type tcDeviceAPI struct {
ID int64 `json:"id,omitempty"`
Device string `json:"device"`
Interface string `json:"interface"`
InBandwidth string `json:"in_bandwidth,omitempty"`
OutBandwidth string `json:"out_bandwidth,omitempty"`
Comment string `json:"comment,omitempty"`
}
func NewTCDeviceResource() resource.Resource { return &tcDeviceResource{} }
func (r *tcDeviceResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_tc_device"
}
func (r *tcDeviceResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "A traffic-shaping device (root qdisc) on a device's interface.",
Attributes: map[string]schema.Attribute{
"id": schema.Int64Attribute{Computed: true, PlanModifiers: []planmodifier.Int64{int64planmodifier.UseStateForUnknown()}},
"device": schema.StringAttribute{Description: "Owning device.", Required: true},
"interface": schema.StringAttribute{Required: true},
"in_bandwidth": schema.StringAttribute{Optional: true},
"out_bandwidth": schema.StringAttribute{Optional: true},
"comment": schema.StringAttribute{Optional: true},
},
}
}
func (r *tcDeviceResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
r.client = configureClient(req, resp)
}
func (r *tcDeviceResource) body(plan tcDeviceModel) tcDeviceAPI {
return tcDeviceAPI{
Device: plan.Device.ValueString(), Interface: plan.Interface.ValueString(),
InBandwidth: plan.InBandwidth.ValueString(), OutBandwidth: plan.OutBandwidth.ValueString(),
Comment: plan.Comment.ValueString(),
}
}
func (r *tcDeviceResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var plan tcDeviceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
var out tcDeviceAPI
if err := r.client.post(ctx, "/api/v1/tc-devices", r.body(plan), &out); err != nil {
resp.Diagnostics.AddError("create tc_device failed", err.Error())
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, r.toModel(out, plan))...)
}
func (r *tcDeviceResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var plan, state tcDeviceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
var out tcDeviceAPI
if err := r.client.post(ctx, "/api/v1/tc-devices", r.body(plan), &out); err != nil {
resp.Diagnostics.AddError("recreate tc_device failed", err.Error())
return
}
if id := state.ID.ValueInt64(); id != 0 {
if err := r.client.del(ctx, "/api/v1/tc-devices/"+strconv.FormatInt(id, 10)); err != nil && !isNotFound(err) {
resp.Diagnostics.AddError("delete old tc_device failed", err.Error())
return
}
}
resp.Diagnostics.Append(resp.State.Set(ctx, r.toModel(out, plan))...)
}
func (r *tcDeviceResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var state tcDeviceModel
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
var out tcDeviceAPI
if err := r.client.get(ctx, "/api/v1/tc-devices/"+strconv.FormatInt(state.ID.ValueInt64(), 10), &out); err != nil {
if isNotFound(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("read tc_device failed", err.Error())
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, r.toModel(out, state))...)
}
func (r *tcDeviceResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var state tcDeviceModel
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
if err := r.client.del(ctx, "/api/v1/tc-devices/"+strconv.FormatInt(state.ID.ValueInt64(), 10)); err != nil && !isNotFound(err) {
resp.Diagnostics.AddError("delete tc_device failed", err.Error())
}
}
func (r *tcDeviceResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
id, err := strconv.ParseInt(req.ID, 10, 64)
if err != nil {
resp.Diagnostics.AddError("invalid import ID", "tc_device id must be an integer")
return
}
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), id)...)
}
func (r *tcDeviceResource) toModel(api tcDeviceAPI, prior tcDeviceModel) tcDeviceModel {
return tcDeviceModel{
ID: types.Int64Value(api.ID), Device: types.StringValue(api.Device), Interface: types.StringValue(api.Interface),
InBandwidth: optionalString(api.InBandwidth, prior.InBandwidth), OutBandwidth: optionalString(api.OutBandwidth, prior.OutBandwidth),
Comment: optionalString(api.Comment, prior.Comment),
}
}