Skip to content

Commit

Permalink
migrate interface_list resource to PluginFramework
Browse files Browse the repository at this point in the history
  • Loading branch information
maksym-nazarenko committed Sep 18, 2023
1 parent 93ed388 commit beeee47
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 90 deletions.
6 changes: 3 additions & 3 deletions client/interface_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/interface_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 0 additions & 1 deletion mikrotik/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
Expand Down
1 change: 1 addition & 0 deletions mikrotik/provider_framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ func (p *ProviderFramework) Resources(ctx context.Context) []func() resource.Res
NewBridgeResource,
NewDhcpLeaseResource,
NewDhcpServerResource,
NewInterfaceListResource,
NewInterfaceWireguardPeerResource,
NewInterfaceWireguardResource,
NewIpv6AddressResource,
Expand Down
159 changes: 77 additions & 82 deletions mikrotik/resource_interface_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
6 changes: 3 additions & 3 deletions mikrotik/resource_interface_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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)
}
Expand Down

0 comments on commit beeee47

Please sign in to comment.