-
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create users in the build-in radius server (#286)
* feat: account resource with data source * Adjust docs and validation * add import test steps * adjust radius capitalization in docs Co-authored-by: Paul Tyng <paul@paultyng.net>
- Loading branch information
Showing
12 changed files
with
520 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "unifi_account Data Source - terraform-provider-unifi" | ||
subcategory: "" | ||
description: |- | ||
unifi_account data source can be used to retrieve RADIUS user accounts | ||
--- | ||
|
||
# unifi_account (Data Source) | ||
|
||
`unifi_account` data source can be used to retrieve RADIUS user accounts | ||
|
||
|
||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `name` (String) The name of the account to look up | ||
|
||
### Optional | ||
|
||
- `site` (String) The name of the site the account is associated with. | ||
|
||
### Read-Only | ||
|
||
- `id` (String) The ID of this account. | ||
- `network_id` (String) ID of the network for this account | ||
- `password` (String, Sensitive) The password of the account. | ||
- `tunnel_medium_type` (Number) See RFC2868 section 3.2 | ||
- `tunnel_type` (Number) See RFC2868 section 3.1 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "unifi_account Resource - terraform-provider-unifi" | ||
subcategory: "" | ||
description: |- | ||
unifi_account manages a RADIUS user account | ||
To authenticate devices based on MAC address, use the MAC address as the username and password under client creation. | ||
Convert lowercase letters to uppercase, and also remove colons or periods from the MAC address. | ||
ATTENTION: If the user profile does not include a VLAN, the client will fall back to the untagged VLAN. | ||
NOTE: MAC-based authentication accounts can only be used for wireless and wired clients. L2TP remote access does not apply. | ||
--- | ||
|
||
# unifi_account (Resource) | ||
|
||
`unifi_account` manages a RADIUS user account | ||
|
||
To authenticate devices based on MAC address, use the MAC address as the username and password under client creation. | ||
Convert lowercase letters to uppercase, and also remove colons or periods from the MAC address. | ||
|
||
ATTENTION: If the user profile does not include a VLAN, the client will fall back to the untagged VLAN. | ||
|
||
NOTE: MAC-based authentication accounts can only be used for wireless and wired clients. L2TP remote access does not apply. | ||
|
||
|
||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `name` (String) The name of the account. | ||
- `password` (String, Sensitive) The password of the account. | ||
|
||
### Optional | ||
|
||
- `network_id` (String) ID of the network for this account | ||
- `site` (String) The name of the site to associate the account with. | ||
- `tunnel_medium_type` (Number) See [RFC 2868](https://www.rfc-editor.org/rfc/rfc2868) section 3.2 Defaults to `6`. | ||
- `tunnel_type` (Number) See [RFC 2868](https://www.rfc-editor.org/rfc/rfc2868) section 3.1 Defaults to `13`. | ||
|
||
### Read-Only | ||
|
||
- `id` (String) The ID of the account. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package provider | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func dataAccount() *schema.Resource { | ||
return &schema.Resource{ | ||
Description: "`unifi_account` data source can be used to retrieve RADIUS user accounts", | ||
|
||
ReadContext: dataAccountRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"id": { | ||
Description: "The ID of this account.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"site": { | ||
Description: "The name of the site the account is associated with.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Optional: true, | ||
}, | ||
"name": { | ||
Description: "The name of the account to look up", | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
|
||
"password": { | ||
Description: "The password of the account.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Sensitive: true, | ||
}, | ||
"tunnel_type": { | ||
Description: "See RFC2868 section 3.1", // @TODO: better documentation https://help.ui.com/hc/en-us/articles/360015268353-UniFi-USG-UDM-Configuring-RADIUS-Server#6 | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
"tunnel_medium_type": { | ||
Description: "See RFC2868 section 3.2", // @TODO: better documentation https://help.ui.com/hc/en-us/articles/360015268353-UniFi-USG-UDM-Configuring-RADIUS-Server#6 | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
"network_id": { | ||
Description: "ID of the network for this account", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataAccountRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
c := meta.(*client) | ||
|
||
name := d.Get("name").(string) | ||
site := d.Get("site").(string) | ||
if site == "" { | ||
site = c.site | ||
} | ||
|
||
accounts, err := c.c.ListAccounts(ctx, site) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
for _, account := range accounts { | ||
if account.Name == name { | ||
d.SetId(account.ID) | ||
d.Set("name", account.Name) | ||
d.Set("password", account.XPassword) | ||
d.Set("tunnel_type", account.TunnelType) | ||
d.Set("tunnel_medium_type", account.TunnelMediumType) | ||
d.Set("network_id", account.NetworkID) | ||
d.Set("site", site) | ||
return nil | ||
} | ||
} | ||
|
||
return diag.Errorf("Account not found with name %s", name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package provider | ||
|
||
import ( | ||
"fmt" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"testing" | ||
) | ||
|
||
func TestAccDataAccount_default(t *testing.T) { | ||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { | ||
preCheck(t) | ||
}, | ||
ProviderFactories: providerFactories, | ||
// TODO: CheckDestroy: , | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataAccountConfig("tfusertest", "secure_1234"), | ||
Check: resource.ComposeTestCheckFunc(), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccDataAccount_mac(t *testing.T) { | ||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { | ||
preCheck(t) | ||
}, | ||
ProviderFactories: providerFactories, | ||
// TODO: CheckDestroy: , | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataMacAccountConfig("00B0D06FC226"), | ||
Check: resource.ComposeTestCheckFunc(), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataAccountConfig(name, password string) string { | ||
return fmt.Sprintf(` | ||
resource "unifi_account" "test" { | ||
name = "%s" | ||
password = "%s" | ||
} | ||
data "unifi_account" "test" { | ||
name = "%s" | ||
depends_on = [ | ||
unifi_account.test | ||
] | ||
} | ||
`, name, password, name) | ||
} | ||
|
||
func testAccDataMacAccountConfig(mac string) string { | ||
return fmt.Sprintf(` | ||
resource "unifi_account" "test" { | ||
name = "%s" | ||
password = "%s" | ||
} | ||
data "unifi_account" "test" { | ||
name = "%s" | ||
depends_on = [ | ||
unifi_account.test | ||
] | ||
} | ||
`, mac, mac, mac) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.