Skip to content

Commit e69a915

Browse files
authored
Add support for wireless.wifi-device
We move on to the wireless stuff. First up is `wireless.wifi-device`. We only handle a small subset of the options, since it's pretty extensive what all this config can do. We'll have to expand this later. But, it should be enough for simpler purposes. Branch: joneshf/add-support-for-wireless-wifi-device Pull-Request: #121
1 parent f07d735 commit e69a915

File tree

10 files changed

+578
-0
lines changed

10 files changed

+578
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "openwrt_wireless_wifi_device Data Source - openwrt"
4+
subcategory: ""
5+
description: |-
6+
The physical radio device.
7+
---
8+
9+
# openwrt_wireless_wifi_device (Data Source)
10+
11+
The physical radio device.
12+
13+
## Example Usage
14+
15+
```terraform
16+
data "openwrt_wireless_wifi_device" "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+
- `band` (String) Channel width. Must be one of: "2g", "5g", "6g".
31+
- `cell_density` (Number) Configures data rates based on the coverage cell density. Must be one of 0, 1, 2, 3.
32+
- `channel` (String) The wireless channel. Currently, only "auto" is supported.
33+
- `country` (String) Two-digit country code. E.g. "US".
34+
- `htmode` (String) Channel width. Must be one of: "HE20", "HE40", "HE80", "HE160", "HT20", "HT40", "HT40-", "HT40+", "NONE", "VHT20", "VHT40", "VHT80", "VHT160".
35+
- `path` (String) Path of the device in `/sys/devices`.
36+
- `type` (String) The type of device. Currently only "mac80211" is supported.
37+
38+
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "openwrt_wireless_wifi_device Resource - openwrt"
4+
subcategory: ""
5+
description: |-
6+
The physical radio device.
7+
---
8+
9+
# openwrt_wireless_wifi_device (Resource)
10+
11+
The physical radio device.
12+
13+
## Example Usage
14+
15+
```terraform
16+
resource "openwrt_wireless_wifi_device" "five_ghz" {
17+
band = "5g"
18+
channel = "auto"
19+
id = "cfg123456"
20+
type = "mac80211"
21+
}
22+
```
23+
24+
<!-- schema generated by tfplugindocs -->
25+
## Schema
26+
27+
### Required
28+
29+
- `channel` (String) The wireless channel. Currently, only "auto" is supported.
30+
- `id` (String) Name of the section. This name is only used when interacting with UCI directly.
31+
- `type` (String) The type of device. Currently only "mac80211" is supported.
32+
33+
### Optional
34+
35+
- `band` (String) Channel width. Must be one of: "2g", "5g", "6g".
36+
- `cell_density` (Number) Configures data rates based on the coverage cell density. Must be one of 0, 1, 2, 3.
37+
- `country` (String) Two-digit country code. E.g. "US".
38+
- `htmode` (String) Channel width. Must be one of: "HE20", "HE40", "HE80", "HE160", "HT20", "HT40", "HT40-", "HT40+", "NONE", "VHT20", "VHT40", "VHT80", "VHT160".
39+
- `path` (String) Path of the device in `/sys/devices`.
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": ["wireless", "wifi-device"]}' \
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+
# "terraformId": "cfg123457",
62+
# }
63+
# ]
64+
#
65+
# We'd then use the information to import the appropriate resource:
66+
67+
terraform import openwrt_wireless_wifi_device.five_ghz cfg123456
68+
```
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
data "openwrt_wireless_wifi_device" "testing" {
2+
id = "testing"
3+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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": ["wireless", "wifi-device"]}' \
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+
# "terraformId": "cfg123457",
17+
# }
18+
# ]
19+
#
20+
# We'd then use the information to import the appropriate resource:
21+
22+
terraform import openwrt_wireless_wifi_device.five_ghz cfg123456
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
resource "openwrt_wireless_wifi_device" "five_ghz" {
2+
band = "5g"
3+
channel = "auto"
4+
id = "cfg123456"
5+
type = "mac80211"
6+
}

internal/acceptancetest/helpers.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const (
2727
acceptanceTestDockerDockerfile = "acceptance-test.Dockerfile"
2828
acceptanceTestDockerName = "joneshf/openwrt"
2929
acceptanceTestDockerHTTPPort = "80/tcp"
30+
acceptanceTestDockerSSHPort = "22/tcp"
3031
acceptanceTestDockerTag = "acceptance-test"
3132

3233
dockerContainerHealthy = "healthy"
@@ -89,6 +90,7 @@ type OpenWrtServer struct {
8990
HTTPPort uint16
9091
Password string
9192
Scheme string
93+
SSHPort uint16
9294
Username string
9395
}
9496

@@ -134,12 +136,17 @@ func RunOpenWrtServer(
134136
intHTTPPort, err := strconv.Atoi(rawHTTPPort)
135137
assert.NilError(t, err)
136138
httpPort := uint16(intHTTPPort)
139+
rawSSHPort := openWrt.GetPort(acceptanceTestDockerSSHPort)
140+
intSSHPort, err := strconv.Atoi(rawSSHPort)
141+
assert.NilError(t, err)
142+
sshPort := uint16(intSSHPort)
137143

138144
return OpenWrtServer{
139145
Hostname: hostname,
140146
HTTPPort: httpPort,
141147
Password: "",
142148
Scheme: "http",
149+
SSHPort: sshPort,
143150
Username: "root",
144151
}
145152
}

openwrt/provider.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/joneshf/terraform-provider-openwrt/openwrt/network/networkswitch"
2424
"github.com/joneshf/terraform-provider-openwrt/openwrt/network/switchvlan"
2525
"github.com/joneshf/terraform-provider-openwrt/openwrt/system/system"
26+
"github.com/joneshf/terraform-provider-openwrt/openwrt/wireless/wifidevice"
2627
)
2728

2829
const (
@@ -161,6 +162,7 @@ func (p *openWrtProvider) DataSources(
161162
networkswitch.NewDataSource,
162163
switchvlan.NewDataSource,
163164
system.NewDataSource,
165+
wifidevice.NewDataSource,
164166
}
165167
}
166168

@@ -185,6 +187,7 @@ func (p *openWrtProvider) Resources(
185187
networkswitch.NewResource,
186188
switchvlan.NewResource,
187189
system.NewResource,
190+
wifidevice.NewResource,
188191
}
189192
}
190193

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//go:build acceptance.test
2+
3+
package wifidevice_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/joneshf/terraform-provider-openwrt/lucirpc"
14+
"github.com/ory/dockertest/v3"
15+
"golang.org/x/crypto/ssh"
16+
"gotest.tools/v3/assert"
17+
)
18+
19+
var (
20+
dockerPool *dockertest.Pool
21+
)
22+
23+
func TestMain(m *testing.M) {
24+
var (
25+
code int
26+
err error
27+
tearDown func()
28+
)
29+
ctx := context.Background()
30+
tearDown, dockerPool, err = acceptancetest.Setup(ctx)
31+
defer func() {
32+
tearDown()
33+
os.Exit(code)
34+
}()
35+
if err != nil {
36+
fmt.Printf("Problem setting up tests: %s", err)
37+
code = 1
38+
return
39+
}
40+
41+
log.Printf("Running tests")
42+
code = m.Run()
43+
}
44+
45+
// runOpenWrtServerWithWireless starts an OpenWrt server,
46+
// and sets up the wireless config.
47+
// Without setting up the config,
48+
// the tests in this package will fail.
49+
func runOpenWrtServerWithWireless(
50+
ctx context.Context,
51+
dockerPool dockertest.Pool,
52+
t *testing.T,
53+
) (*lucirpc.Client, string) {
54+
openWrtServer := acceptancetest.RunOpenWrtServer(
55+
ctx,
56+
dockerPool,
57+
t,
58+
)
59+
sshURL := fmt.Sprintf("%s:%d", openWrtServer.Hostname, openWrtServer.SSHPort)
60+
sshConfig := &ssh.ClientConfig{
61+
Auth: []ssh.AuthMethod{
62+
ssh.Password(openWrtServer.Password),
63+
},
64+
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
65+
User: openWrtServer.Username,
66+
}
67+
sshClient, err := ssh.Dial("tcp", sshURL, sshConfig)
68+
assert.NilError(t, err)
69+
t.Cleanup(func() {
70+
sshClient.Close()
71+
})
72+
session, err := sshClient.NewSession()
73+
assert.NilError(t, err)
74+
t.Cleanup(func() {
75+
session.Close()
76+
})
77+
err = session.Run("touch /etc/config/wireless")
78+
assert.NilError(t, err)
79+
client, err := lucirpc.NewClient(
80+
ctx,
81+
openWrtServer.Scheme,
82+
openWrtServer.Hostname,
83+
openWrtServer.HTTPPort,
84+
openWrtServer.Username,
85+
openWrtServer.Password,
86+
)
87+
assert.NilError(t, err)
88+
providerBlock := openWrtServer.ProviderBlock()
89+
return client, providerBlock
90+
}

0 commit comments

Comments
 (0)