Add support for unifi_network datasource

#168
This commit is contained in:
Paul Tyng
2021-08-30 21:14:11 -04:00
parent 6258533310
commit 361d52e5bd
4 changed files with 119 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "unifi_network Data Source - terraform-provider-unifi"
subcategory: ""
description: |-
unifi_network data source can be used to retrieve the ID for a network by name.
---
# unifi_network (Data Source)
`unifi_network` data source can be used to retrieve the ID for a network by name.
<!-- schema generated by tfplugindocs -->
## Schema
### Optional
- **name** (String) The name of the user group to look up. Defaults to `Default`.
- **site** (String) The name of the site the user group is associated with.
### Read-Only
- **id** (String) The ID of this AP group.

View File

@@ -0,0 +1,62 @@
package provider
import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataNetwork() *schema.Resource {
return &schema.Resource{
Description: "`unifi_network` data source can be used to retrieve the ID for a network by name.",
ReadContext: dataNetworkRead,
Schema: map[string]*schema.Schema{
"id": {
Description: "The ID of this AP group.",
Type: schema.TypeString,
Computed: true,
},
"site": {
Description: "The name of the site the user group is associated with.",
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"name": {
Description: "The name of the user group to look up.",
Type: schema.TypeString,
Optional: true,
Default: "Default",
},
},
}
}
func dataNetworkRead(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
}
networks, err := c.c.ListNetwork(ctx, site)
if err != nil {
return diag.FromErr(err)
}
for _, n := range networks {
if n.Name == name {
d.SetId(n.ID)
d.Set("site", site)
return nil
}
}
return diag.Errorf("network not found with name %s", name)
}

View File

@@ -0,0 +1,29 @@
package provider
import (
"testing"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)
func TestAccDataNetwork_default(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { preCheck(t) },
ProviderFactories: providerFactories,
// TODO: CheckDestroy: ,
Steps: []resource.TestStep{
{
Config: testAccDataNetworkConfig_default,
Check: resource.ComposeTestCheckFunc(
// testCheckNetworkExists(t, "name"),
),
},
},
})
}
const testAccDataNetworkConfig_default = `
data "unifi_network" "lan" {
name = "LAN"
}
`

View File

@@ -77,6 +77,7 @@ func New(version string) func() *schema.Provider {
},
DataSourcesMap: map[string]*schema.Resource{
"unifi_ap_group": dataAPGroup(),
"unifi_network": dataNetwork(),
"unifi_port_profile": dataPortProfile(),
"unifi_radius_profile": dataRADIUSProfile(),
"unifi_user_group": dataUserGroup(),