github.com/dim13/unifi@v0.0.0-20230308161331-9b04946f5e93/site.go (about) 1 // Copyright (c) 2014 The unifi Authors. All rights reserved. 2 // Use of this source code is governed by ISC-style license 3 // that can be found in the LICENSE file. 4 5 package unifi 6 7 import "fmt" 8 9 // UniFi Site object 10 type Site struct { 11 ID string `json:"_id"` // For internal use 12 AttrHiddenID string `json:"attr_hidden_id"` 13 AttrNoDelete bool `json:"attr_no_delete"` 14 Desc string // The name of the site! (Friendly name) 15 Name string // The site-id! 16 Role string 17 } 18 19 // Site returns a site with given name or description 20 func (u *Unifi) Site(desc string) (*Site, error) { 21 22 sites, err := u.Sites() 23 if err != nil { 24 return nil, err 25 } 26 27 // First, search site by description (friendly name) 28 for _, s := range sites { 29 if s.Desc == desc { 30 return &s, nil 31 } 32 } 33 34 // If not found, search site by name (id used in url) 35 for _, s := range sites { 36 if s.Name == desc { 37 return &s, nil 38 } 39 } 40 41 return nil, fmt.Errorf("Site %s not found", desc) 42 } 43 44 // Sites returns a slice with all sites of the controller 45 func (u *Unifi) Sites() ([]Site, error) { 46 var response struct { 47 Data []Site 48 Meta meta 49 } 50 err := u.parse(nil, "self/sites", nil, &response) 51 return response.Data, err 52 } 53 54 /*// SiteNameByDesc returns the name (id) of a site, searched by its description (user friendly name) 55 // So far only for internal use 56 func (u *Unifi) siteNameByDesc(desc string) (string, error) { 57 sites, err := u.Sites() 58 if err != nil { 59 return "", err 60 } 61 62 for _, s := range sites { 63 if s.Desc == desc { 64 return s.Name, nil 65 } 66 } 67 68 return "", errors.New("No site with desc: " + desc) 69 } */