Skip to content

Commit

Permalink
Add support for wireless.wifi-device
Browse files Browse the repository at this point in the history
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
  • Loading branch information
joneshf authored Apr 3, 2023
1 parent f07d735 commit e69a915
Show file tree
Hide file tree
Showing 10 changed files with 578 additions and 0 deletions.
38 changes: 38 additions & 0 deletions docs/data-sources/wireless_wifi_device.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "openwrt_wireless_wifi_device Data Source - openwrt"
subcategory: ""
description: |-
The physical radio device.
---

# openwrt_wireless_wifi_device (Data Source)

The physical radio device.

## Example Usage

```terraform
data "openwrt_wireless_wifi_device" "testing" {
id = "testing"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `id` (String) Name of the section. This name is only used when interacting with UCI directly.

### Read-Only

- `band` (String) Channel width. Must be one of: "2g", "5g", "6g".
- `cell_density` (Number) Configures data rates based on the coverage cell density. Must be one of 0, 1, 2, 3.
- `channel` (String) The wireless channel. Currently, only "auto" is supported.
- `country` (String) Two-digit country code. E.g. "US".
- `htmode` (String) Channel width. Must be one of: "HE20", "HE40", "HE80", "HE160", "HT20", "HT40", "HT40-", "HT40+", "NONE", "VHT20", "VHT40", "VHT80", "VHT160".
- `path` (String) Path of the device in `/sys/devices`.
- `type` (String) The type of device. Currently only "mac80211" is supported.


68 changes: 68 additions & 0 deletions docs/resources/wireless_wifi_device.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "openwrt_wireless_wifi_device Resource - openwrt"
subcategory: ""
description: |-
The physical radio device.
---

# openwrt_wireless_wifi_device (Resource)

The physical radio device.

## Example Usage

```terraform
resource "openwrt_wireless_wifi_device" "five_ghz" {
band = "5g"
channel = "auto"
id = "cfg123456"
type = "mac80211"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `channel` (String) The wireless channel. Currently, only "auto" is supported.
- `id` (String) Name of the section. This name is only used when interacting with UCI directly.
- `type` (String) The type of device. Currently only "mac80211" is supported.

### Optional

- `band` (String) Channel width. Must be one of: "2g", "5g", "6g".
- `cell_density` (Number) Configures data rates based on the coverage cell density. Must be one of 0, 1, 2, 3.
- `country` (String) Two-digit country code. E.g. "US".
- `htmode` (String) Channel width. Must be one of: "HE20", "HE40", "HE80", "HE160", "HT20", "HT40", "HT40-", "HT40+", "NONE", "VHT20", "VHT40", "VHT80", "VHT160".
- `path` (String) Path of the device in `/sys/devices`.

## Import

Import is supported using the following syntax:

```shell
# Find the Terraform id from LuCI's JSON-RPC API.
# One way to find this information is with `curl` and `jq`:
#
# curl \
# --data '{"id": 0, "method": "foreach", "params": ["wireless", "wifi-device"]}' \
# http://192.168.1.1/cgi-bin/luci/rpc/uci?auth=$AUTH_TOKEN \
# | jq '.result | map({terraformId: .[".name"]})'
#
# This command will output something like:
#
# [
# {
# "terraformId": "cfg123456",
# },
# {
# "terraformId": "cfg123457",
# }
# ]
#
# We'd then use the information to import the appropriate resource:

terraform import openwrt_wireless_wifi_device.five_ghz cfg123456
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "openwrt_wireless_wifi_device" "testing" {
id = "testing"
}
22 changes: 22 additions & 0 deletions examples/resources/openwrt_wireless_wifi_device/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Find the Terraform id from LuCI's JSON-RPC API.
# One way to find this information is with `curl` and `jq`:
#
# curl \
# --data '{"id": 0, "method": "foreach", "params": ["wireless", "wifi-device"]}' \
# http://192.168.1.1/cgi-bin/luci/rpc/uci?auth=$AUTH_TOKEN \
# | jq '.result | map({terraformId: .[".name"]})'
#
# This command will output something like:
#
# [
# {
# "terraformId": "cfg123456",
# },
# {
# "terraformId": "cfg123457",
# }
# ]
#
# We'd then use the information to import the appropriate resource:

terraform import openwrt_wireless_wifi_device.five_ghz cfg123456
6 changes: 6 additions & 0 deletions examples/resources/openwrt_wireless_wifi_device/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
resource "openwrt_wireless_wifi_device" "five_ghz" {
band = "5g"
channel = "auto"
id = "cfg123456"
type = "mac80211"
}
7 changes: 7 additions & 0 deletions internal/acceptancetest/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
acceptanceTestDockerDockerfile = "acceptance-test.Dockerfile"
acceptanceTestDockerName = "joneshf/openwrt"
acceptanceTestDockerHTTPPort = "80/tcp"
acceptanceTestDockerSSHPort = "22/tcp"
acceptanceTestDockerTag = "acceptance-test"

dockerContainerHealthy = "healthy"
Expand Down Expand Up @@ -89,6 +90,7 @@ type OpenWrtServer struct {
HTTPPort uint16
Password string
Scheme string
SSHPort uint16
Username string
}

Expand Down Expand Up @@ -134,12 +136,17 @@ func RunOpenWrtServer(
intHTTPPort, err := strconv.Atoi(rawHTTPPort)
assert.NilError(t, err)
httpPort := uint16(intHTTPPort)
rawSSHPort := openWrt.GetPort(acceptanceTestDockerSSHPort)
intSSHPort, err := strconv.Atoi(rawSSHPort)
assert.NilError(t, err)
sshPort := uint16(intSSHPort)

return OpenWrtServer{
Hostname: hostname,
HTTPPort: httpPort,
Password: "",
Scheme: "http",
SSHPort: sshPort,
Username: "root",
}
}
Expand Down
3 changes: 3 additions & 0 deletions openwrt/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/joneshf/terraform-provider-openwrt/openwrt/network/networkswitch"
"github.com/joneshf/terraform-provider-openwrt/openwrt/network/switchvlan"
"github.com/joneshf/terraform-provider-openwrt/openwrt/system/system"
"github.com/joneshf/terraform-provider-openwrt/openwrt/wireless/wifidevice"
)

const (
Expand Down Expand Up @@ -161,6 +162,7 @@ func (p *openWrtProvider) DataSources(
networkswitch.NewDataSource,
switchvlan.NewDataSource,
system.NewDataSource,
wifidevice.NewDataSource,
}
}

Expand All @@ -185,6 +187,7 @@ func (p *openWrtProvider) Resources(
networkswitch.NewResource,
switchvlan.NewResource,
system.NewResource,
wifidevice.NewResource,
}
}

Expand Down
90 changes: 90 additions & 0 deletions openwrt/wireless/wifidevice/acceptance_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
//go:build acceptance.test

package wifidevice_test

import (
"context"
"fmt"
"log"
"os"
"testing"

"github.com/joneshf/terraform-provider-openwrt/internal/acceptancetest"
"github.com/joneshf/terraform-provider-openwrt/lucirpc"
"github.com/ory/dockertest/v3"
"golang.org/x/crypto/ssh"
"gotest.tools/v3/assert"
)

var (
dockerPool *dockertest.Pool
)

func TestMain(m *testing.M) {
var (
code int
err error
tearDown func()
)
ctx := context.Background()
tearDown, dockerPool, err = acceptancetest.Setup(ctx)
defer func() {
tearDown()
os.Exit(code)
}()
if err != nil {
fmt.Printf("Problem setting up tests: %s", err)
code = 1
return
}

log.Printf("Running tests")
code = m.Run()
}

// runOpenWrtServerWithWireless starts an OpenWrt server,
// and sets up the wireless config.
// Without setting up the config,
// the tests in this package will fail.
func runOpenWrtServerWithWireless(
ctx context.Context,
dockerPool dockertest.Pool,
t *testing.T,
) (*lucirpc.Client, string) {
openWrtServer := acceptancetest.RunOpenWrtServer(
ctx,
dockerPool,
t,
)
sshURL := fmt.Sprintf("%s:%d", openWrtServer.Hostname, openWrtServer.SSHPort)
sshConfig := &ssh.ClientConfig{
Auth: []ssh.AuthMethod{
ssh.Password(openWrtServer.Password),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
User: openWrtServer.Username,
}
sshClient, err := ssh.Dial("tcp", sshURL, sshConfig)
assert.NilError(t, err)
t.Cleanup(func() {
sshClient.Close()
})
session, err := sshClient.NewSession()
assert.NilError(t, err)
t.Cleanup(func() {
session.Close()
})
err = session.Run("touch /etc/config/wireless")
assert.NilError(t, err)
client, err := lucirpc.NewClient(
ctx,
openWrtServer.Scheme,
openWrtServer.Hostname,
openWrtServer.HTTPPort,
openWrtServer.Username,
openWrtServer.Password,
)
assert.NilError(t, err)
providerBlock := openWrtServer.ProviderBlock()
return client, providerBlock
}
Loading

0 comments on commit e69a915

Please sign in to comment.