Skip to content

Commit caa9d2a

Browse files
authored
Add support for dhcp.odhcpd
This seems to be one of the few resources which is not configurable through LuCI. We still do want to support it, though. So we add support for it. It seems like we've added all the options. But if there are any that are missing, we can add them later. Branch: joneshf/add-support-for-dhcp-odhcpd Pull-Request: #128
1 parent bd3e38d commit caa9d2a

File tree

9 files changed

+426
-0
lines changed

9 files changed

+426
-0
lines changed

docs/data-sources/dhcp_odhcpd.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "openwrt_dhcp_odhcpd Data Source - openwrt"
4+
subcategory: ""
5+
description: |-
6+
An embedded DHCP/DHCPv6/RA server & NDP relay.
7+
---
8+
9+
# openwrt_dhcp_odhcpd (Data Source)
10+
11+
An embedded DHCP/DHCPv6/RA server & NDP relay.
12+
13+
## Example Usage
14+
15+
```terraform
16+
data "openwrt_dhcp_odhcpd" "testing" {
17+
id = "testing"
18+
}
19+
```
20+
21+
<!-- schema generated by tfplugindocs -->
22+
## Schema
23+
24+
### Required
25+
26+
- `id` (String) Name of the section. This name is only used when interacting with UCI directly.
27+
28+
### Read-Only
29+
30+
- `leasefile` (String) Location of the lease/hostfile for DHCPv4 and DHCPv6.
31+
- `leasetrigger` (String) Location of the lease trigger script.
32+
- `legacy` (Boolean) Enable DHCPv4 if the 'dhcp' section constains a `start` option, but no `dhcpv4` option set.
33+
- `loglevel` (Number) Syslog level priority (0-7).
34+
- `maindhcp` (Boolean) Use odhcpd as the main DHCPv4 service.
35+
36+

docs/resources/dhcp_odhcpd.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "openwrt_dhcp_odhcpd Resource - openwrt"
4+
subcategory: ""
5+
description: |-
6+
An embedded DHCP/DHCPv6/RA server & NDP relay.
7+
---
8+
9+
# openwrt_dhcp_odhcpd (Resource)
10+
11+
An embedded DHCP/DHCPv6/RA server & NDP relay.
12+
13+
## Example Usage
14+
15+
```terraform
16+
resource "openwrt_dhcp_odhcpd" "this" {
17+
id = "testing"
18+
leasefile = "/tmp/leasefile"
19+
leasetrigger = "/tmp/leasetrigger"
20+
legacy = true
21+
loglevel = 6
22+
maindhcp = true
23+
}
24+
```
25+
26+
<!-- schema generated by tfplugindocs -->
27+
## Schema
28+
29+
### Required
30+
31+
- `id` (String) Name of the section. This name is only used when interacting with UCI directly.
32+
33+
### Optional
34+
35+
- `leasefile` (String) Location of the lease/hostfile for DHCPv4 and DHCPv6.
36+
- `leasetrigger` (String) Location of the lease trigger script.
37+
- `legacy` (Boolean) Enable DHCPv4 if the 'dhcp' section constains a `start` option, but no `dhcpv4` option set.
38+
- `loglevel` (Number) Syslog level priority (0-7).
39+
- `maindhcp` (Boolean) Use odhcpd as the main DHCPv4 service.
40+
41+
## Import
42+
43+
Import is supported using the following syntax:
44+
45+
```shell
46+
# Find the Terraform id from LuCI's JSON-RPC API.
47+
# One way to find this information is with `curl` and `jq`:
48+
#
49+
# curl \
50+
# --data '{"id": 0, "method": "foreach", "params": ["dhcp", "odhcpd"]}' \
51+
# http://192.168.1.1/cgi-bin/luci/rpc/uci?auth=$AUTH_TOKEN \
52+
# | jq '.result | map({terraformId: .[".name"]})'
53+
#
54+
# This command will output something like:
55+
#
56+
# [
57+
# {
58+
# "terraformId": "cfg123456",
59+
# }
60+
# ]
61+
#
62+
# We'd then use the information to import the appropriate resource:
63+
64+
terraform import openwrt_dhcp_odhcpd.this cfg123456
65+
```
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
data "openwrt_dhcp_odhcpd" "testing" {
2+
id = "testing"
3+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Find the Terraform id from LuCI's JSON-RPC API.
2+
# One way to find this information is with `curl` and `jq`:
3+
#
4+
# curl \
5+
# --data '{"id": 0, "method": "foreach", "params": ["dhcp", "odhcpd"]}' \
6+
# http://192.168.1.1/cgi-bin/luci/rpc/uci?auth=$AUTH_TOKEN \
7+
# | jq '.result | map({terraformId: .[".name"]})'
8+
#
9+
# This command will output something like:
10+
#
11+
# [
12+
# {
13+
# "terraformId": "cfg123456",
14+
# }
15+
# ]
16+
#
17+
# We'd then use the information to import the appropriate resource:
18+
19+
terraform import openwrt_dhcp_odhcpd.this cfg123456
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
resource "openwrt_dhcp_odhcpd" "this" {
2+
id = "testing"
3+
leasefile = "/tmp/leasefile"
4+
leasetrigger = "/tmp/leasetrigger"
5+
legacy = true
6+
loglevel = 6
7+
maindhcp = true
8+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//go:build acceptance.test
2+
3+
package odhcpd_test
4+
5+
import (
6+
"context"
7+
"fmt"
8+
"log"
9+
"os"
10+
"testing"
11+
12+
"github.com/joneshf/terraform-provider-openwrt/internal/acceptancetest"
13+
"github.com/ory/dockertest/v3"
14+
)
15+
16+
var (
17+
dockerPool *dockertest.Pool
18+
)
19+
20+
func TestMain(m *testing.M) {
21+
var (
22+
code int
23+
err error
24+
tearDown func()
25+
)
26+
ctx := context.Background()
27+
tearDown, dockerPool, err = acceptancetest.Setup(ctx)
28+
defer func() {
29+
tearDown()
30+
os.Exit(code)
31+
}()
32+
if err != nil {
33+
fmt.Printf("Problem setting up tests: %s", err)
34+
code = 1
35+
return
36+
}
37+
38+
log.Printf("Running tests")
39+
code = m.Run()
40+
}

openwrt/dhcp/odhcpd/odhcpd.go

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package odhcpd
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
5+
"github.com/hashicorp/terraform-plugin-framework/datasource"
6+
"github.com/hashicorp/terraform-plugin-framework/resource"
7+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
8+
"github.com/hashicorp/terraform-plugin-framework/types"
9+
"github.com/joneshf/terraform-provider-openwrt/lucirpc"
10+
"github.com/joneshf/terraform-provider-openwrt/openwrt/internal/lucirpcglue"
11+
)
12+
13+
const (
14+
leaseFileAttribute = "leasefile"
15+
leaseFileAttributeDescription = "Location of the lease/hostfile for DHCPv4 and DHCPv6."
16+
leaseFileUCIOption = "leasefile"
17+
18+
leaseTriggerAttribute = "leasetrigger"
19+
leaseTriggerAttributeDescription = "Location of the lease trigger script."
20+
leaseTriggerUCIOption = "leasetrigger"
21+
22+
legacyAttribute = "legacy"
23+
legacyAttributeDescription = "Enable DHCPv4 if the 'dhcp' section constains a `start` option, but no `dhcpv4` option set."
24+
legacyUCIOption = "legacy"
25+
26+
logLevelAttribute = "loglevel"
27+
logLevelAttributeDescription = "Syslog level priority (0-7)."
28+
logLevelUCIOption = "loglevel"
29+
30+
mainDHCPAttribute = "maindhcp"
31+
mainDHCPAttributeDescription = "Use odhcpd as the main DHCPv4 service."
32+
mainDHCPUCIOption = "maindhcp"
33+
34+
schemaDescription = "An embedded DHCP/DHCPv6/RA server & NDP relay."
35+
36+
uciConfig = "dhcp"
37+
uciType = "odhcpd"
38+
)
39+
40+
var (
41+
leaseFileSchemaAttribute = lucirpcglue.StringSchemaAttribute[model, lucirpc.Options, lucirpc.Options]{
42+
Description: leaseFileAttributeDescription,
43+
ReadResponse: lucirpcglue.ReadResponseOptionString(modelSetLeaseFile, leaseFileAttribute, leaseFileUCIOption),
44+
ResourceExistence: lucirpcglue.NoValidation,
45+
UpsertRequest: lucirpcglue.UpsertRequestOptionString(modelGetLeaseFile, leaseFileAttribute, leaseFileUCIOption),
46+
}
47+
48+
leaseTriggerSchemaAttribute = lucirpcglue.StringSchemaAttribute[model, lucirpc.Options, lucirpc.Options]{
49+
Description: leaseTriggerAttributeDescription,
50+
ReadResponse: lucirpcglue.ReadResponseOptionString(modelSetLeaseTrigger, leaseTriggerAttribute, leaseTriggerUCIOption),
51+
ResourceExistence: lucirpcglue.NoValidation,
52+
UpsertRequest: lucirpcglue.UpsertRequestOptionString(modelGetLeaseTrigger, leaseTriggerAttribute, leaseTriggerUCIOption),
53+
}
54+
55+
legacySchemaAttribute = lucirpcglue.BoolSchemaAttribute[model, lucirpc.Options, lucirpc.Options]{
56+
Description: legacyAttributeDescription,
57+
ReadResponse: lucirpcglue.ReadResponseOptionBool(modelSetLegacy, legacyAttribute, legacyUCIOption),
58+
ResourceExistence: lucirpcglue.NoValidation,
59+
UpsertRequest: lucirpcglue.UpsertRequestOptionBool(modelGetLegacy, legacyAttribute, legacyUCIOption),
60+
}
61+
62+
logLevelSchemaAttribute = lucirpcglue.Int64SchemaAttribute[model, lucirpc.Options, lucirpc.Options]{
63+
Description: logLevelAttributeDescription,
64+
ReadResponse: lucirpcglue.ReadResponseOptionInt64(modelSetLogLevel, logLevelAttribute, logLevelUCIOption),
65+
ResourceExistence: lucirpcglue.NoValidation,
66+
UpsertRequest: lucirpcglue.UpsertRequestOptionInt64(modelGetLogLevel, logLevelAttribute, logLevelUCIOption),
67+
Validators: []validator.Int64{
68+
int64validator.Between(0, 7),
69+
},
70+
}
71+
72+
mainDHCPSchemaAttribute = lucirpcglue.BoolSchemaAttribute[model, lucirpc.Options, lucirpc.Options]{
73+
Description: mainDHCPAttributeDescription,
74+
ReadResponse: lucirpcglue.ReadResponseOptionBool(modelSetMainDHCP, mainDHCPAttribute, mainDHCPUCIOption),
75+
ResourceExistence: lucirpcglue.NoValidation,
76+
UpsertRequest: lucirpcglue.UpsertRequestOptionBool(modelGetMainDHCP, mainDHCPAttribute, mainDHCPUCIOption),
77+
}
78+
79+
schemaAttributes = map[string]lucirpcglue.SchemaAttribute[model, lucirpc.Options, lucirpc.Options]{
80+
leaseFileAttribute: leaseFileSchemaAttribute,
81+
leaseTriggerAttribute: leaseTriggerSchemaAttribute,
82+
legacyAttribute: legacySchemaAttribute,
83+
logLevelAttribute: logLevelSchemaAttribute,
84+
lucirpcglue.IdAttribute: lucirpcglue.IdSchemaAttribute(modelGetId, modelSetId),
85+
mainDHCPAttribute: mainDHCPSchemaAttribute,
86+
}
87+
)
88+
89+
func NewDataSource() datasource.DataSource {
90+
return lucirpcglue.NewDataSource(
91+
modelGetId,
92+
schemaAttributes,
93+
schemaDescription,
94+
uciConfig,
95+
uciType,
96+
)
97+
}
98+
99+
func NewResource() resource.Resource {
100+
return lucirpcglue.NewResource(
101+
modelGetId,
102+
schemaAttributes,
103+
schemaDescription,
104+
uciConfig,
105+
uciType,
106+
)
107+
}
108+
109+
type model struct {
110+
Id types.String `tfsdk:"id"`
111+
LeaseFile types.String `tfsdk:"leasefile"`
112+
LeaseTrigger types.String `tfsdk:"leasetrigger"`
113+
Legacy types.Bool `tfsdk:"legacy"`
114+
LogLevel types.Int64 `tfsdk:"loglevel"`
115+
MainDHCP types.Bool `tfsdk:"maindhcp"`
116+
}
117+
118+
func modelGetId(m model) types.String { return m.Id }
119+
func modelGetLeaseFile(m model) types.String { return m.LeaseFile }
120+
func modelGetLeaseTrigger(m model) types.String { return m.LeaseTrigger }
121+
func modelGetLegacy(m model) types.Bool { return m.Legacy }
122+
func modelGetLogLevel(m model) types.Int64 { return m.LogLevel }
123+
func modelGetMainDHCP(m model) types.Bool { return m.MainDHCP }
124+
125+
func modelSetId(m *model, value types.String) { m.Id = value }
126+
func modelSetLeaseFile(m *model, value types.String) { m.LeaseFile = value }
127+
func modelSetLeaseTrigger(m *model, value types.String) { m.LeaseTrigger = value }
128+
func modelSetLegacy(m *model, value types.Bool) { m.Legacy = value }
129+
func modelSetLogLevel(m *model, value types.Int64) { m.LogLevel = value }
130+
func modelSetMainDHCP(m *model, value types.Bool) { m.MainDHCP = value }

0 commit comments

Comments
 (0)