Skip to content
This repository was archived by the owner on Apr 30, 2026. It is now read-only.

Commit 0a8c9dd

Browse files
committed
Add support for unifi_network datasource
#168
1 parent 992c17b commit 0a8c9dd

4 files changed

Lines changed: 119 additions & 0 deletions

File tree

docs/data-sources/network.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "unifi_network Data Source - terraform-provider-unifi"
4+
subcategory: ""
5+
description: |-
6+
unifi_network data source can be used to retrieve the ID for a network by name.
7+
---
8+
9+
# unifi_network (Data Source)
10+
11+
`unifi_network` data source can be used to retrieve the ID for a network by name.
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Optional
19+
20+
- **name** (String) The name of the user group to look up. Defaults to `Default`.
21+
- **site** (String) The name of the site the user group is associated with.
22+
23+
### Read-Only
24+
25+
- **id** (String) The ID of this AP group.
26+
27+

internal/provider/data_network.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
)
9+
10+
func dataNetwork() *schema.Resource {
11+
return &schema.Resource{
12+
Description: "`unifi_network` data source can be used to retrieve the ID for a network by name.",
13+
14+
ReadContext: dataNetworkRead,
15+
16+
Schema: map[string]*schema.Schema{
17+
"id": {
18+
Description: "The ID of this AP group.",
19+
Type: schema.TypeString,
20+
Computed: true,
21+
},
22+
"site": {
23+
Description: "The name of the site the user group is associated with.",
24+
Type: schema.TypeString,
25+
Computed: true,
26+
Optional: true,
27+
},
28+
"name": {
29+
Description: "The name of the user group to look up.",
30+
Type: schema.TypeString,
31+
Optional: true,
32+
Default: "Default",
33+
},
34+
},
35+
}
36+
}
37+
38+
func dataNetworkRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
39+
c := meta.(*client)
40+
41+
name := d.Get("name").(string)
42+
site := d.Get("site").(string)
43+
if site == "" {
44+
site = c.site
45+
}
46+
47+
networks, err := c.c.ListNetwork(ctx, site)
48+
if err != nil {
49+
return diag.FromErr(err)
50+
}
51+
for _, n := range networks {
52+
if n.Name == name {
53+
d.SetId(n.ID)
54+
55+
d.Set("site", site)
56+
57+
return nil
58+
}
59+
}
60+
61+
return diag.Errorf("network not found with name %s", name)
62+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package provider
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccDataNetwork_default(t *testing.T) {
10+
resource.ParallelTest(t, resource.TestCase{
11+
PreCheck: func() { preCheck(t) },
12+
ProviderFactories: providerFactories,
13+
// TODO: CheckDestroy: ,
14+
Steps: []resource.TestStep{
15+
{
16+
Config: testAccDataNetworkConfig_default,
17+
Check: resource.ComposeTestCheckFunc(
18+
// testCheckNetworkExists(t, "name"),
19+
),
20+
},
21+
},
22+
})
23+
}
24+
25+
const testAccDataNetworkConfig_default = `
26+
data "unifi_network" "lan" {
27+
name = "LAN"
28+
}
29+
`

internal/provider/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ func New(version string) func() *schema.Provider {
7777
},
7878
DataSourcesMap: map[string]*schema.Resource{
7979
"unifi_ap_group": dataAPGroup(),
80+
"unifi_network": dataNetwork(),
8081
"unifi_port_profile": dataPortProfile(),
8182
"unifi_radius_profile": dataRADIUSProfile(),
8283
"unifi_user_group": dataUserGroup(),

0 commit comments

Comments
 (0)