Skip to content

Commit

Permalink
Merge branch 'master' into refactor/resource-interface_list_member
Browse files Browse the repository at this point in the history
  • Loading branch information
maksym-nazarenko committed Sep 19, 2023
2 parents d5dcbd9 + 9c2a0ac commit 4aea419
Show file tree
Hide file tree
Showing 13 changed files with 297 additions and 346 deletions.
10 changes: 5 additions & 5 deletions client/bridge_port.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (

// BridgePort defines port-in-bridge association
type BridgePort struct {
Id string `mikrotik:".id"`
Bridge string `mikrotik:"bridge"`
Interface string `mikrotik:"interface"`
PVId int `mikrotik:"pvid"`
Comment string `mikrotik:"comment"`
Id string `mikrotik:".id" codegen:"id,mikrotikID"`
Bridge string `mikrotik:"bridge" codegen:"bridge"`
Interface string `mikrotik:"interface" codegen:"interface"`
PVId int `mikrotik:"pvid" codegen:"pvid"`
Comment string `mikrotik:"comment" codegen:"comment"`
}

var _ Resource = (*BridgePort)(nil)
Expand Down
10 changes: 5 additions & 5 deletions client/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
)

type DnsRecord struct {
Id string `mikrotik:".id"`
Name string `mikrotik:"name"`
Ttl types.MikrotikDuration `mikrotik:"ttl"`
Address string `mikrotik:"address"`
Comment string `mikrotik:"comment"`
Id string `mikrotik:".id" codegen:"id,mikrotikID"`
Name string `mikrotik:"name" codegen:"name,terraformID,required"`
Ttl types.MikrotikDuration `mikrotik:"ttl" codegen:"ttl"`
Address string `mikrotik:"address" codegen:"address,required"`
Comment string `mikrotik:"comment" codegen:"comment"`
}

func (d *DnsRecord) ActionToCommand(action Action) string {
Expand Down
12 changes: 6 additions & 6 deletions client/ip_addr.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
)

type IpAddress struct {
Id string `mikrotik:".id"`
Address string `mikrotik:"address"`
Comment string `mikrotik:"comment"`
Disabled bool `mikrotik:"disabled"`
Interface string `mikrotik:"interface"`
Network string `mikrotik:"network"`
Id string `mikrotik:".id" codegen:"id,mikrotikID"`
Address string `mikrotik:"address" codegen:"address,required"`
Comment string `mikrotik:"comment" codegen:"comment"`
Disabled bool `mikrotik:"disabled" codegen:"disabled"`
Interface string `mikrotik:"interface" codegen:"interface,required"`
Network string `mikrotik:"network" codegen:"network,computed"`
}

var _ Resource = (*IpAddress)(nil)
Expand Down
6 changes: 3 additions & 3 deletions docs/resources/bridge_port.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ resource mikrotik_bridge_port "eth2port" {

- `bridge` (String) The bridge interface the respective interface is grouped in.
- `comment` (String) Short description for this association.
- `interface` (String) Name of the interface. Default: `*0`.
- `pvid` (Number) Port VLAN ID (pvid) specifies which VLAN the untagged ingress traffic is assigned to. This property only has effect when vlan-filtering is set to yes. Default: `1`.
- `interface` (String) Name of the interface.
- `pvid` (Number) Port VLAN ID (pvid) specifies which VLAN the untagged ingress traffic is assigned to. This property only has effect when vlan-filtering is set to yes.

### Read-Only

- `id` (String) The ID of this resource.
- `id` (String) Unique ID for the instance.

## Import
Import is supported using the following syntax:
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/dns_record.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ resource "mikrotik_dns_record" "record" {

### 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
4 changes: 2 additions & 2 deletions docs/resources/ip_address.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ resource "mikrotik_ip_address" "lan" {
### Optional

- `comment` (String) The comment for the IP address assignment.
- `disabled` (Boolean) Whether to disable IP address. Default: `false`.
- `disabled` (Boolean) Whether to disable IP address.

### Read-Only

- `id` (String) The ID of this resource.
- `id` (String) Unique ID of this resource.
- `network` (String) IP address for the network.

## Import
Expand Down
5 changes: 1 addition & 4 deletions mikrotik/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,10 @@ func Provider(client *mt.Mikrotik) *schema.Provider {
},
ResourcesMap: map[string]*schema.Resource{
"mikrotik_bgp_instance": resourceBgpInstance(),
"mikrotik_bridge_port": resourceBridgePort(),
"mikrotik_bridge_vlan": resourceBridgeVlan(),
"mikrotik_dhcp_server_network": resourceDhcpServerNetwork(),
"mikrotik_dns_record": resourceRecord(),
"mikrotik_interface_list": resourceInterfaceList(),
"mikrotik_ip_address": resourceIpAddress(),
"mikrotik_firewall_filter_rule": resourceFirewallFilterRule(),
"mikrotik_interface_list": resourceInterfaceList(),
},
}

Expand Down
3 changes: 3 additions & 0 deletions mikrotik/provider_framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,15 @@ func (p *ProviderFramework) DataSources(ctx context.Context) []func() datasource
func (p *ProviderFramework) Resources(ctx context.Context) []func() resource.Resource {
return []func() resource.Resource{
NewBgpPeerResource,
NewBridgePortResource,
NewBridgeResource,
NewDhcpLeaseResource,
NewDhcpServerResource,
NewDnsRecordResource,
NewInterfaceListMemberResource,
NewInterfaceWireguardPeerResource,
NewInterfaceWireguardResource,
NewIpAddressResource,
NewIpv6AddressResource,
NewPoolResource,
NewSchedulerResource,
Expand Down
182 changes: 89 additions & 93 deletions mikrotik/resource_bridge_port.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,126 +5,122 @@ import (

"github.com/ddelnano/terraform-provider-mikrotik/client"
"github.com/ddelnano/terraform-provider-mikrotik/mikrotik/internal/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
"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/schema/validator"

tftypes "github.com/hashicorp/terraform-plugin-framework/types"
)

func resourceBridgePort() *schema.Resource {
return &schema.Resource{
Description: "Manages ports in bridge associations.",
type bridgePort struct {
client *client.Mikrotik
}

CreateContext: resourceBridgePortCreate,
ReadContext: resourceBridgePortRead,
UpdateContext: resourceBridgePortUpdate,
DeleteContext: resourceBridgePortDelete,
// Ensure the implementation satisfies the expected interfaces.
var (
_ resource.Resource = &bridgePort{}
_ resource.ResourceWithConfigure = &bridgePort{}
_ resource.ResourceWithImportState = &bridgePort{}
)

Importer: &schema.ResourceImporter{
StateContext: utils.ImportStateContextUppercaseWrapper(schema.ImportStatePassthroughContext),
},
// NewBridgePortResource is a helper function to simplify the provider implementation.
func NewBridgePortResource() resource.Resource {
return &bridgePort{}
}

func (r *bridgePort) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
if req.ProviderData == nil {
return
}

Schema: map[string]*schema.Schema{
"bridge": {
Type: schema.TypeString,
r.client = req.ProviderData.(*client.Mikrotik)
}

// Metadata returns the resource type name.
func (r *bridgePort) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_bridge_port"
}

// Schema defines the schema for the resource.
func (s *bridgePort) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Manages ports in bridge associations.",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
Description: "Unique ID for the instance.",
},
"bridge": schema.StringAttribute{
Optional: true,
Computed: true,
Description: "The bridge interface the respective interface is grouped in.",
},
"interface": {
Type: schema.TypeString,
"interface": schema.StringAttribute{
Optional: true,
Default: "*0",
Computed: true,
Description: "Name of the interface.",
},
"pvid": {
Type: schema.TypeInt,
Optional: true,
Default: 1,
ValidateFunc: validation.IntBetween(1, 4094),
Description: "Port VLAN ID (pvid) specifies which VLAN the untagged ingress traffic is assigned to. This property only has effect when vlan-filtering is set to yes.",
"pvid": schema.Int64Attribute{
Optional: true,
Computed: true,
Validators: []validator.Int64{
int64validator.Between(1, 4094),
},
Description: "Port VLAN ID (pvid) specifies which VLAN the untagged ingress traffic is assigned to. This property only has effect when vlan-filtering is set to yes.",
},
"comment": {
Type: schema.TypeString,
"comment": schema.StringAttribute{
Optional: true,
Computed: true,
Description: "Short description for this association.",
},
},
}
}

func resourceBridgePortCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*client.Mikrotik)
bridgePort, err := c.AddBridgePort(dataToBridgePort(d))
if err != nil {
return diag.FromErr(err)
}
recordBridgePortToData(bridgePort, d)

return resourceBridgePortRead(ctx, d, m)
// Create creates the resource and sets the initial Terraform state.
func (r *bridgePort) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var terraformModel bridgePortModel
var mikrotikModel client.BridgePort
GenericCreateResource(&terraformModel, &mikrotikModel, r.client)(ctx, req, resp)
}

func resourceBridgePortRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*client.Mikrotik)
bridgePort, err := c.FindBridgePort(d.Id())
if client.IsNotFoundError(err) {
d.SetId("")
return nil
}
if err != nil {
return diag.FromErr(err)
}

return recordBridgePortToData(bridgePort, d)
// Read refreshes the Terraform state with the latest data.
func (r *bridgePort) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var terraformModel bridgePortModel
var mikrotikModel client.BridgePort
GenericReadResource(&terraformModel, &mikrotikModel, r.client)(ctx, req, resp)
}

func resourceBridgePortUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
c := m.(*client.Mikrotik)
bridgePort := dataToBridgePort(d)
_, err := c.UpdateBridgePort(bridgePort)
if err != nil {
return diag.FromErr(err)
}

return diags
// Update updates the resource and sets the updated Terraform state on success.
func (r *bridgePort) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var terraformModel bridgePortModel
var mikrotikModel client.BridgePort
GenericUpdateResource(&terraformModel, &mikrotikModel, r.client)(ctx, req, resp)
}

func resourceBridgePortDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
c := m.(*client.Mikrotik)
err := c.DeleteBridgePort(d.Id())
if err != nil {
return diag.FromErr(err)
}

return diags
// Delete deletes the resource and removes the Terraform state on success.
func (r *bridgePort) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var terraformModel bridgePortModel
var mikrotikModel client.BridgePort
GenericDeleteResource(&terraformModel, &mikrotikModel, r.client)(ctx, req, resp)
}

func dataToBridgePort(d *schema.ResourceData) *client.BridgePort {
return &client.BridgePort{
Id: d.Id(),
Bridge: d.Get("bridge").(string),
Interface: d.Get("interface").(string),
PVId: d.Get("pvid").(int),
Comment: d.Get("comment").(string),
}
func (r *bridgePort) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
// Retrieve import ID and save to id attribute
utils.ImportUppercaseWrapper(resource.ImportStatePassthroughID)(ctx, path.Root("id"), req, resp)
}

func recordBridgePortToData(r *client.BridgePort, d *schema.ResourceData) diag.Diagnostics {
var diags diag.Diagnostics

if err := d.Set("bridge", r.Bridge); err != nil {
diags = append(diags, diag.FromErr(err)...)
}
if err := d.Set("interface", r.Interface); err != nil {
diags = append(diags, diag.FromErr(err)...)
}
if err := d.Set("pvid", r.PVId); 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.Id)

return diags
type bridgePortModel struct {
Id tftypes.String `tfsdk:"id"`
Bridge tftypes.String `tfsdk:"bridge"`
Interface tftypes.String `tfsdk:"interface"`
PVId tftypes.Int64 `tfsdk:"pvid"`
Comment tftypes.String `tfsdk:"comment"`
}
5 changes: 5 additions & 0 deletions mikrotik/resource_bridge_port_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ func TestBridgePort_basic(t *testing.T) {
resource.TestCheckResourceAttr(rStatePath, "comment", "updated resource"),
),
},
{
ImportState: true,
ResourceName: rStatePath,
ImportStateVerify: true,
},
},
})
}
Expand Down
Loading

0 comments on commit 4aea419

Please sign in to comment.