From beeee475ac56d40737272be1327018bbea127458 Mon Sep 17 00:00:00 2001 From: Maksym Nazarenko Date: Mon, 18 Sep 2023 23:19:24 +0300 Subject: [PATCH] migrate interface_list resource to PluginFramework --- client/interface_list.go | 6 +- docs/resources/interface_list.md | 2 +- mikrotik/provider.go | 1 - mikrotik/provider_framework.go | 1 + mikrotik/resource_interface_list.go | 159 +++++++++++------------ mikrotik/resource_interface_list_test.go | 6 +- 6 files changed, 85 insertions(+), 90 deletions(-) diff --git a/client/interface_list.go b/client/interface_list.go index f0e02b38..34f2737d 100644 --- a/client/interface_list.go +++ b/client/interface_list.go @@ -6,9 +6,9 @@ import ( // InterfaceList manages a list of interfaces type InterfaceList struct { - Id string `mikrotik:".id"` - Comment string `mikrotik:"comment"` - Name string `mikrotik:"name"` + Id string `mikrotik:".id" codegen:"id,mikrotikID"` + Comment string `mikrotik:"comment" codegen:"comment"` + Name string `mikrotik:"name" codegen:"name,terraformID,required"` } var _ Resource = (*InterfaceList)(nil) diff --git a/docs/resources/interface_list.md b/docs/resources/interface_list.md index feba0ce6..6b90dfa5 100644 --- a/docs/resources/interface_list.md +++ b/docs/resources/interface_list.md @@ -22,7 +22,7 @@ resource "mikrotik_interface_list" "default" { ### Read-Only -- `id` (String) The ID of this resource. +- `id` (String) Unique ID of this resource. ## Import Import is supported using the following syntax: diff --git a/mikrotik/provider.go b/mikrotik/provider.go index 512047c5..c1fd9f03 100644 --- a/mikrotik/provider.go +++ b/mikrotik/provider.go @@ -70,7 +70,6 @@ func Provider(client *mt.Mikrotik) *schema.Provider { "mikrotik_dhcp_server_network": resourceDhcpServerNetwork(), "mikrotik_dns_record": resourceRecord(), "mikrotik_interface_list_member": resourceInterfaceListMember(), - "mikrotik_interface_list": resourceInterfaceList(), "mikrotik_ip_address": resourceIpAddress(), "mikrotik_firewall_filter_rule": resourceFirewallFilterRule(), }, diff --git a/mikrotik/provider_framework.go b/mikrotik/provider_framework.go index b3939343..083be71b 100644 --- a/mikrotik/provider_framework.go +++ b/mikrotik/provider_framework.go @@ -186,6 +186,7 @@ func (p *ProviderFramework) Resources(ctx context.Context) []func() resource.Res NewBridgeResource, NewDhcpLeaseResource, NewDhcpServerResource, + NewInterfaceListResource, NewInterfaceWireguardPeerResource, NewInterfaceWireguardResource, NewIpv6AddressResource, diff --git a/mikrotik/resource_interface_list.go b/mikrotik/resource_interface_list.go index 01cd96fa..de7fec0d 100644 --- a/mikrotik/resource_interface_list.go +++ b/mikrotik/resource_interface_list.go @@ -4,109 +4,104 @@ import ( "context" "github.com/ddelnano/terraform-provider-mikrotik/client" - "github.com/hashicorp/terraform-plugin-sdk/v2/diag" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "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" + + tftypes "github.com/hashicorp/terraform-plugin-framework/types" ) -func resourceInterfaceList() *schema.Resource { - return &schema.Resource{ - Description: "Allows to define set of interfaces for easier interface management.", +type interfaceList struct { + client *client.Mikrotik +} - CreateContext: resourceInterfaceListCreate, - ReadContext: resourceInterfaceListRead, - UpdateContext: resourceInterfaceListUpdate, - DeleteContext: resourceInterfaceListDelete, +// Ensure the implementation satisfies the expected interfaces. +var ( + _ resource.Resource = &interfaceList{} + _ resource.ResourceWithConfigure = &interfaceList{} + _ resource.ResourceWithImportState = &interfaceList{} +) - Importer: &schema.ResourceImporter{ - StateContext: schema.ImportStatePassthroughContext, - }, +// NewInterfaceListResource is a helper function to simplify the provider implementation. +func NewInterfaceListResource() resource.Resource { + return &interfaceList{} +} - Schema: map[string]*schema.Schema{ - "name": { - Type: schema.TypeString, - Required: true, - Description: "Name of the interface list.", +func (r *interfaceList) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { + if req.ProviderData == nil { + return + } + + r.client = req.ProviderData.(*client.Mikrotik) +} + +// Metadata returns the resource type name. +func (r *interfaceList) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_interface_list" +} + +// Schema defines the schema for the resource. +func (s *interfaceList) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { + resp.Schema = schema.Schema{ + Description: "Allows to define set of interfaces for easier interface management.", + Attributes: map[string]schema.Attribute{ + "id": schema.StringAttribute{ + Computed: true, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.UseStateForUnknown(), + }, + Description: "Unique ID of this resource.", }, - "comment": { - Type: schema.TypeString, + "comment": schema.StringAttribute{ Optional: true, + Computed: true, Description: "Comment to this list.", }, + "name": schema.StringAttribute{ + Required: true, + Description: "Name of the interface list.", + }, }, } } -func resourceInterfaceListCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - c := m.(*client.Mikrotik) - r := dataToInterfaceList(d) - record, err := c.AddInterfaceList(r) - if err != nil { - return diag.FromErr(err) - } - d.SetId(record.Name) - - return resourceInterfaceListRead(ctx, d, m) +// Create creates the resource and sets the initial Terraform state. +func (r *interfaceList) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { + var terraformModel interfaceListModel + var mikrotikModel client.InterfaceList + GenericCreateResource(&terraformModel, &mikrotikModel, r.client)(ctx, req, resp) } -func resourceInterfaceListRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - c := m.(*client.Mikrotik) - record, err := c.FindInterfaceList(d.Id()) - if err != nil { - return diag.FromErr(err) - } - - return recordInterfaceListToData(record, d) +// Read refreshes the Terraform state with the latest data. +func (r *interfaceList) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { + var terraformModel interfaceListModel + var mikrotikModel client.InterfaceList + GenericReadResource(&terraformModel, &mikrotikModel, r.client)(ctx, req, resp) } -func resourceInterfaceListUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - c := m.(*client.Mikrotik) - currentRecord, err := c.FindInterfaceList(d.Id()) - if err != nil { - return diag.FromErr(err) - } - - r := dataToInterfaceList(d) - r.Id = currentRecord.Id - - _, err = c.UpdateInterfaceList(r) - if err != nil { - return diag.FromErr(err) - } - d.SetId(r.Name) - - return resourceInterfaceListRead(ctx, d, m) +// Update updates the resource and sets the updated Terraform state on success. +func (r *interfaceList) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + var terraformModel interfaceListModel + var mikrotikModel client.InterfaceList + GenericUpdateResource(&terraformModel, &mikrotikModel, r.client)(ctx, req, resp) } -func resourceInterfaceListDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - c := m.(*client.Mikrotik) - err := c.DeleteInterfaceList(d.Id()) - if err != nil { - return diag.FromErr(err) - } - - return nil +// Delete deletes the resource and removes the Terraform state on success. +func (r *interfaceList) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { + var terraformModel interfaceListModel + var mikrotikModel client.InterfaceList + GenericDeleteResource(&terraformModel, &mikrotikModel, r.client)(ctx, req, resp) } -func dataToInterfaceList(d *schema.ResourceData) *client.InterfaceList { - return &client.InterfaceList{ - Id: d.Id(), - Name: d.Get("name").(string), - Comment: d.Get("comment").(string), - } +func (r *interfaceList) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { + // Retrieve import ID and save to id attribute + resource.ImportStatePassthroughID(ctx, path.Root("name"), req, resp) } -func recordInterfaceListToData(r *client.InterfaceList, d *schema.ResourceData) diag.Diagnostics { - var diags diag.Diagnostics - - if err := d.Set("name", r.Name); err != nil { - diags = append(diags, diag.FromErr(err)...) - } - - if err := d.Set("comment", r.Comment); err != nil { - diags = append(diags, diag.FromErr(err)...) - } - - d.SetId(r.Name) - - return diags +type interfaceListModel struct { + Id tftypes.String `tfsdk:"id"` + Comment tftypes.String `tfsdk:"comment"` + Name tftypes.String `tfsdk:"name"` } diff --git a/mikrotik/resource_interface_list_test.go b/mikrotik/resource_interface_list_test.go index 0cf54178..770b4ea2 100644 --- a/mikrotik/resource_interface_list_test.go +++ b/mikrotik/resource_interface_list_test.go @@ -43,13 +43,13 @@ func testAccCheckInterfaceListDestroy(s *terraform.State) error { continue } - remoteRecord, err := c.FindInterfaceList(rs.Primary.ID) + remoteRecord, err := c.FindInterfaceList(rs.Primary.Attributes["name"]) if !client.IsNotFoundError(err) && err != nil { return err } if remoteRecord != nil { - return fmt.Errorf("remote record (%s) still exists", remoteRecord.Id) + return fmt.Errorf("remote record (%s) still exists", remoteRecord.Name) } } @@ -68,7 +68,7 @@ func testAccInterfaceListExists(resourceName string) resource.TestCheckFunc { } c := client.NewClient(client.GetConfigFromEnv()) - record, err := c.FindInterfaceList(rs.Primary.ID) + record, err := c.FindInterfaceList(rs.Primary.Attributes["name"]) if err != nil { return fmt.Errorf("Unable to get remote record for %s: %v", resourceName, err) }