Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DHCP leases in adguard_config datasource #98

Merged
merged 5 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 30 additions & 15 deletions adguard/config_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ type dhcpStatusModel struct {
Interface types.String `tfsdk:"interface"`
Ipv4Settings types.Object `tfsdk:"ipv4_settings"`
Ipv6Settings types.Object `tfsdk:"ipv6_settings"`
Leases types.Set `tfsdk:"leases"`
Leases types.List `tfsdk:"leases"`
StaticLeases types.Set `tfsdk:"static_leases"`
}

Expand All @@ -231,7 +231,7 @@ func (o dhcpStatusModel) attrTypes() map[string]attr.Type {
"interface": types.StringType,
"ipv4_settings": types.ObjectType{AttrTypes: dhcpIpv4Model{}.attrTypes()},
"ipv6_settings": types.ObjectType{AttrTypes: dhcpIpv6Model{}.attrTypes()},
"leases": types.SetType{ElemType: types.ObjectType{AttrTypes: dhcpLeasesModel{}.attrTypes()}},
"leases": types.ListType{ElemType: types.ObjectType{AttrTypes: dhcpLeasesModel{}.attrTypes()}},
"static_leases": types.SetType{ElemType: types.ObjectType{AttrTypes: dhcpStaticLeasesModel{}.attrTypes()}},
}
}
Expand Down Expand Up @@ -322,19 +322,19 @@ func (o dhcpIpv6Model) defaultObject() map[string]attr.Value {

// dhcpLeasesModel maps DHCP leases schema data
type dhcpLeasesModel struct {
Mac types.String `tfsdk:"mac"`
Ip types.String `tfsdk:"ip"`
Hostname types.String `tfsdk:"hostname"`
Expiration types.String `tfsdk:"expiration"`
Mac types.String `tfsdk:"mac"`
Ip types.String `tfsdk:"ip"`
Hostname types.String `tfsdk:"hostname"`
Expires types.String `tfsdk:"expires"`
}

// attrTypes - return attribute types for this model
func (o dhcpLeasesModel) attrTypes() map[string]attr.Type {
return map[string]attr.Type{
"mac": types.StringType,
"ip": types.StringType,
"hostname": types.StringType,
"expiration": types.StringType,
"mac": types.StringType,
"ip": types.StringType,
"hostname": types.StringType,
"expires": types.StringType,
}
}

Expand Down Expand Up @@ -933,11 +933,26 @@ func (o *configCommonModel) Read(ctx context.Context, adg adguard.ADG, currState
stateDhcpStatus.Ipv4Settings = stateDhcpConfig.Ipv4Settings
stateDhcpStatus.Ipv6Settings = stateDhcpConfig.Ipv6Settings
stateDhcpStatus.StaticLeases = stateDhcpConfig.StaticLeases
// add the extra attributes for the data source
stateDhcpStatus.Leases, d = types.SetValueFrom(ctx, types.ObjectType{AttrTypes: dhcpLeasesModel{}.attrTypes()}, dhcpStatus.Leases)
diags.Append(d...)
if diags.HasError() {
return
if len(dhcpStatus.Leases) > 0 {
// need to go through all entries to create a slice
var dhcpLeases []dhcpLeasesModel
var stateDhcpConfigLease dhcpLeasesModel
for _, dhcpLease := range dhcpStatus.Leases {
stateDhcpConfigLease.Mac = types.StringValue(dhcpLease.Mac)
stateDhcpConfigLease.Ip = types.StringValue(dhcpLease.Ip)
stateDhcpConfigLease.Hostname = types.StringValue(dhcpLease.Hostname)
stateDhcpConfigLease.Expires = types.StringValue(dhcpLease.Expires)
dhcpLeases = append(dhcpLeases, stateDhcpConfigLease)
}
// convert to a set
stateDhcpStatus.Leases, d = types.ListValueFrom(ctx, types.ObjectType{AttrTypes: dhcpLeasesModel{}.attrTypes()}, dhcpLeases)
diags.Append(d...)
if diags.HasError() {
return
}
} else {
// use a null set
stateDhcpStatus.Leases = types.ListNull(types.ObjectType{AttrTypes: dhcpLeasesModel{}.attrTypes()})
}
// add to config model
o.Dhcp, d = types.ObjectValueFrom(ctx, dhcpStatusModel{}.attrTypes(), &stateDhcpStatus)
Expand Down
4 changes: 2 additions & 2 deletions adguard/config_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ func (d *configDataSource) Schema(_ context.Context, _ datasource.SchemaRequest,
},
},
},
"leases": schema.SetNestedAttribute{
"leases": schema.ListNestedAttribute{
Description: "Current leases in the DHCP server",
Computed: true,
NestedObject: schema.NestedAttributeObject{
Expand All @@ -302,7 +302,7 @@ func (d *configDataSource) Schema(_ context.Context, _ datasource.SchemaRequest,
Description: "Hostname associated with the lease",
Computed: true,
},
"expiration": schema.StringAttribute{
"expires": schema.StringAttribute{
Description: "Expiration timestamp for the lease",
Computed: true,
},
Expand Down
2 changes: 2 additions & 0 deletions adguard/config_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ func TestAccConfigDataSource(t *testing.T) {
resource.TestCheckResourceAttr("data.adguard_config.test", "dhcp.ipv4_settings.range_end", "192.168.200.50"),
resource.TestCheckResourceAttr("data.adguard_config.test", "dhcp.ipv4_settings.lease_duration", "3600"),
resource.TestCheckResourceAttr("data.adguard_config.test", "dhcp.ipv6_settings.lease_duration", "86400"),
resource.TestCheckResourceAttr("data.adguard_config.test", "dhcp.leases.#", "3"),
resource.TestCheckResourceAttr("data.adguard_config.test", "dhcp.leases.1.hostname", "dynamic-lease-2"),
resource.TestCheckResourceAttr("data.adguard_config.test", "dhcp.static_leases.#", "0"),
resource.TestCheckResourceAttr("data.adguard_config.test", "tls.enabled", "true"),
resource.TestCheckResourceAttr("data.adguard_config.test", "tls.server_name", "TestAdGuardHome"),
Expand Down
26 changes: 26 additions & 0 deletions docker/data/leases.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"version": 1,
"leases": [
{
"mac": "00:aa:bb:cc:dd:ee",
"expires": "2034-01-10T20:30:16Z",
"hostname": "dynamic-lease-1",
"ip": "192.168.200.32",
"static": false
},
{
"mac": "ff:ee:dd:cc:bb:aa",
"expires": "2034-01-11T20:01:14Z",
"hostname": "dynamic-lease-2",
"ip": "192.168.200.34",
"static": false
},
{
"mac": "ab:cd:ef:01:23:45",
"expires": "2034-01-10T20:31:40Z",
"hostname": "dynamic-lease-3",
"ip": "192.168.200.44",
"static": false
}
]
}
1 change: 1 addition & 0 deletions docker/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ services:
restart: unless-stopped
volumes:
- "./conf:/opt/adguardhome/conf"
- "./data:/opt/adguardhome/work/data"
- "./ssl:/opt/adguardhome/ssl"
ports:
- "8080:8080/tcp"
Expand Down
4 changes: 2 additions & 2 deletions docs/data-sources/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ Read-Only:
- `interface` (String) The interface to use for the DHCP server
- `ipv4_settings` (Attributes) (see [below for nested schema](#nestedatt--dhcp--ipv4_settings))
- `ipv6_settings` (Attributes) (see [below for nested schema](#nestedatt--dhcp--ipv6_settings))
- `leases` (Attributes Set) Current leases in the DHCP server (see [below for nested schema](#nestedatt--dhcp--leases))
- `leases` (Attributes List) Current leases in the DHCP server (see [below for nested schema](#nestedatt--dhcp--leases))
- `static_leases` (Attributes Set) Current static leases in the DHCP server (see [below for nested schema](#nestedatt--dhcp--static_leases))

<a id="nestedatt--dhcp--ipv4_settings"></a>
Expand Down Expand Up @@ -152,7 +152,7 @@ Read-Only:

Read-Only:

- `expiration` (String) Expiration timestamp for the lease
- `expires` (String) Expiration timestamp for the lease
- `hostname` (String) Hostname associated with the lease
- `ip` (String) IP address associated with the lease
- `mac` (String) MAC address associated with the lease
Expand Down