|
| 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 | +} |
0 commit comments