Skip to content

Commit

Permalink
feat: Data source to list DHCP leases
Browse files Browse the repository at this point in the history
Fixes #316
  • Loading branch information
vaerh committed Dec 15, 2023
1 parent 9acc3cc commit 71f9571
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
data "routeros_ip_dhcp_server_leases" "data" {}
94 changes: 94 additions & 0 deletions routeros/datasource_ip_dhcp_server_leases.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package routeros

// Script generated from sampled device MikroTik 7.11.2 (stable) on CHR AMD-x86_64

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func DatasourceIpDhcpServerLeases() *schema.Resource {
return &schema.Resource{
ReadContext: datasourceIpDhcpServerLeasesRead,
Schema: map[string]*schema.Schema{
MetaResourcePath: PropResourcePath("/ip/dhcp-server/lease"),
MetaId: PropId(Id),

KeyFilter: PropFilterRw,
"data": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": { // Sample = .id: "*1"
Type: schema.TypeString,
Computed: true,
},
"address": { // Sample = address: "192.168.0.1"
Type: schema.TypeString,
Computed: true,
},
"address_lists": { // Sample = address-lists: ""
Type: schema.TypeString,
Computed: true,
},
"blocked": { // Sample = blocked: "false"
Type: schema.TypeBool,
Computed: true,
},
"comment": { // Sample = comment: "server1 "
Type: schema.TypeString,
Computed: true,
},
"dhcp_option": { // Sample = dhcp-option: ""
Type: schema.TypeString,
Computed: true,
},
"disabled": { // Sample = disabled: "true"
Type: schema.TypeBool,
Computed: true,
},
"dynamic": { // Sample = dynamic: "false"
Type: schema.TypeBool,
Computed: true,
},
"last_seen": { // Sample = last-seen: "never"
Type: schema.TypeString,
Computed: true,
},
"mac_address": { // Sample = mac-address: "00:0C:29:00:01:A0"
Type: schema.TypeString,
Computed: true,
},
"radius": { // Sample = radius: "false"
Type: schema.TypeBool,
Computed: true,
},
"server": { // Sample = server: "bridge_dhcp_lan"
Type: schema.TypeString,
Computed: true,
},
"status": { // Sample = status: "waiting"
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}

func datasourceIpDhcpServerLeasesRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
s := DatasourceIpDhcpServerLeases().Schema
path := s[MetaResourcePath].Default.(string)

res, err := ReadItemsFiltered(buildReadFilter(d.Get(KeyFilter).(map[string]interface{})), path, m.(Client))
if err != nil {
return diag.FromErr(err)
}

return MikrotikResourceDataToTerraformDatasource(res, "data", s, d)
}
15 changes: 8 additions & 7 deletions routeros/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,14 @@ func Provider() *schema.Provider {
"routeros_move_items": ResourceMoveItems(),
},
DataSourcesMap: map[string]*schema.Resource{
"routeros_firewall": DatasourceFirewall(),
"routeros_interfaces": DatasourceInterfaces(),
"routeros_ip_addresses": DatasourceIPAddresses(),
"routeros_ip_arp": DatasourceIpArp(),
"routeros_ip_routes": DatasourceIPRoutes(),
"routeros_ipv6_addresses": DatasourceIPv6Addresses(),
"routeros_system_resource": DatasourceSystemResource(),
"routeros_firewall": DatasourceFirewall(),
"routeros_interfaces": DatasourceInterfaces(),
"routeros_ip_addresses": DatasourceIPAddresses(),
"routeros_ip_arp": DatasourceIpArp(),
"routeros_ip_dhcp_server_leases": DatasourceIpDhcpServerLeases(),
"routeros_ip_routes": DatasourceIPRoutes(),
"routeros_ipv6_addresses": DatasourceIPv6Addresses(),
"routeros_system_resource": DatasourceSystemResource(),
},
ConfigureContextFunc: NewClient,
}
Expand Down

0 comments on commit 71f9571

Please sign in to comment.