Create a Port Profile data source

This commit is contained in:
James Toyer
2021-02-15 21:49:48 +00:00
committed by Paul Tyng
parent 052f60d77e
commit 8b96dac8fa
3 changed files with 120 additions and 0 deletions

View File

@@ -0,0 +1,62 @@
package provider
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataPortProfile() *schema.Resource {
return &schema.Resource{
Description: "`unifi_port_profile` data source can be used to retrieve the ID for a port profile by name.",
Read: dataPortProfileRead,
Schema: map[string]*schema.Schema{
"id": {
Description: "The ID of this port profile.",
Type: schema.TypeString,
Computed: true,
},
"site": {
Description: "The name of the site the port profile is associated with.",
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"name": {
Description: "The name of the port profile to look up.",
Type: schema.TypeString,
Optional: true,
Default: "Default",
},
},
}
}
func dataPortProfileRead(d *schema.ResourceData, meta interface{}) error {
c := meta.(*client)
name := d.Get("name").(string)
site := d.Get("site").(string)
if site == "" {
site = c.site
}
groups, err := c.c.ListPortProfile(context.TODO(), site)
if err != nil {
return err
}
for _, g := range groups {
if g.Name == name {
d.SetId(g.ID)
d.Set("site", site)
return nil
}
}
return fmt.Errorf("port profile not found with name %s", name)
}

View File

@@ -0,0 +1,57 @@
package provider
import (
"testing"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func TestAccDataPortProfile_default(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { preCheck(t) },
ProviderFactories: providerFactories,
// TODO: CheckDestroy: ,
Steps: []resource.TestStep{
{
Config: testAccDataPortProfileConfig_default,
Check: resource.ComposeTestCheckFunc(),
},
},
})
}
func TestAccDataPortProfile_multiple_providers(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { preCheck(t) },
ProviderFactories: map[string]func() (*schema.Provider, error){
"unifi2": func() (*schema.Provider, error) {
return New("acctest")(), nil
},
"unifi3": func() (*schema.Provider, error) {
return New("acctest")(), nil
},
},
// TODO: CheckDestroy: ,
Steps: []resource.TestStep{
{
Config: `
data "unifi_port_profile" "unifi2" {
provider = "unifi2"
}
data "unifi_port_profile" "unifi3" {
provider = "unifi3"
}
`,
Check: resource.ComposeTestCheckFunc(
// testCheckNetworkExists(t, "name"),
),
},
},
})
}
const testAccDataPortProfileConfig_default = `
data "unifi_port_profile" "default" {
}
`

View File

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