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