Merge pull request #822 from soflane/feature/default-site-name-override

Add support for default_site_name_override to customize default site names
This commit is contained in:
Cody Lee 2025-04-30 05:07:23 -05:00 committed by GitHub
commit 4068c8c5c4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 40 additions and 22 deletions

View File

@ -152,6 +152,9 @@
# A setting of ["all"] will poll all sites; this works if you only have 1 site too. # A setting of ["all"] will poll all sites; this works if you only have 1 site too.
sites = ["all"] sites = ["all"]
# Added an example for overriding the default site name.
# default_site_name_override = "My Custom Default Site"
# Specify a timeout, leave missing to declare infinite wait. This determines the maximum # Specify a timeout, leave missing to declare infinite wait. This determines the maximum
# time to wait for a response from the unifi controller on any API request. # time to wait for a response from the unifi controller on any API request.
# timeout = 60s # timeout = 60s

View File

@ -42,7 +42,7 @@
"tags": [ "tags": [
"customer:abcde" "customer:abcde"
] ]
} },
"unifi": { "unifi": {
"dynamic": false, "dynamic": false,
@ -59,7 +59,8 @@
"save_dpi": false, "save_dpi": false,
"save_sites": true, "save_sites": true,
"hash_pii": false, "hash_pii": false,
"verify_ssl": false "verify_ssl": false,
"default_site_name_override": "My Custom Default Site"
}, },
"controllers": [ "controllers": [
{ {

View File

@ -60,7 +60,8 @@ unifi:
save_sites: true save_sites: true
hash_pii: false hash_pii: false
verify_ssl: false verify_ssl: false
# Added an example for overriding the default site name.
# default_site_name_override: "My Custom Default Site"
controllers: controllers:
# Repeat the following stanza to poll multiple controllers. # Repeat the following stanza to poll multiple controllers.

View File

@ -278,7 +278,14 @@ func (u *InputUnifi) getFilteredSites(c *Controller) ([]*unifi.Site, error) {
sites, err := c.Unifi.GetSites() sites, err := c.Unifi.GetSites()
if err != nil { if err != nil {
return nil, fmt.Errorf("controller: %w", err) return nil, fmt.Errorf("controller: %w", err)
} else if len(c.Sites) == 0 || StringInSlice("all", c.Sites) { }
// Apply the default_site_name_override to the first site in the list, if configured.
if len(sites) > 0 && c.DefaultSiteNameOverride != "" {
sites[0].Name = c.DefaultSiteNameOverride
}
if len(c.Sites) == 0 || StringInSlice("all", c.Sites) {
return sites, nil return sites, nil
} }

View File

@ -34,24 +34,25 @@ type InputUnifi struct {
// Controller represents the configuration for a UniFi Controller. // Controller represents the configuration for a UniFi Controller.
// Each polled controller may have its own configuration. // Each polled controller may have its own configuration.
type Controller struct { type Controller struct {
VerifySSL *bool `json:"verify_ssl" toml:"verify_ssl" xml:"verify_ssl" yaml:"verify_ssl"` VerifySSL *bool `json:"verify_ssl" toml:"verify_ssl" xml:"verify_ssl" yaml:"verify_ssl"`
SaveAnomal *bool `json:"save_anomalies" toml:"save_anomalies" xml:"save_anomalies" yaml:"save_anomalies"` SaveAnomal *bool `json:"save_anomalies" toml:"save_anomalies" xml:"save_anomalies" yaml:"save_anomalies"`
SaveAlarms *bool `json:"save_alarms" toml:"save_alarms" xml:"save_alarms" yaml:"save_alarms"` SaveAlarms *bool `json:"save_alarms" toml:"save_alarms" xml:"save_alarms" yaml:"save_alarms"`
SaveEvents *bool `json:"save_events" toml:"save_events" xml:"save_events" yaml:"save_events"` SaveEvents *bool `json:"save_events" toml:"save_events" xml:"save_events" yaml:"save_events"`
SaveIDs *bool `json:"save_ids" toml:"save_ids" xml:"save_ids" yaml:"save_ids"` SaveIDs *bool `json:"save_ids" toml:"save_ids" xml:"save_ids" yaml:"save_ids"`
SaveDPI *bool `json:"save_dpi" toml:"save_dpi" xml:"save_dpi" yaml:"save_dpi"` SaveDPI *bool `json:"save_dpi" toml:"save_dpi" xml:"save_dpi" yaml:"save_dpi"`
SaveRogue *bool `json:"save_rogue" toml:"save_rogue" xml:"save_rogue" yaml:"save_rogue"` SaveRogue *bool `json:"save_rogue" toml:"save_rogue" xml:"save_rogue" yaml:"save_rogue"`
HashPII *bool `json:"hash_pii" toml:"hash_pii" xml:"hash_pii" yaml:"hash_pii"` HashPII *bool `json:"hash_pii" toml:"hash_pii" xml:"hash_pii" yaml:"hash_pii"`
DropPII *bool `json:"drop_pii" toml:"drop_pii" xml:"drop_pii" yaml:"drop_pii"` DropPII *bool `json:"drop_pii" toml:"drop_pii" xml:"drop_pii" yaml:"drop_pii"`
SaveSites *bool `json:"save_sites" toml:"save_sites" xml:"save_sites" yaml:"save_sites"` SaveSites *bool `json:"save_sites" toml:"save_sites" xml:"save_sites" yaml:"save_sites"`
CertPaths []string `json:"ssl_cert_paths" toml:"ssl_cert_paths" xml:"ssl_cert_path" yaml:"ssl_cert_paths"` CertPaths []string `json:"ssl_cert_paths" toml:"ssl_cert_paths" xml:"ssl_cert_path" yaml:"ssl_cert_paths"`
User string `json:"user" toml:"user" xml:"user" yaml:"user"` User string `json:"user" toml:"user" xml:"user" yaml:"user"`
Pass string `json:"pass" toml:"pass" xml:"pass" yaml:"pass"` Pass string `json:"pass" toml:"pass" xml:"pass" yaml:"pass"`
APIKey string `json:"api_key" toml:"api_key" xml:"api_key" yaml:"api_key"` APIKey string `json:"api_key" toml:"api_key" xml:"api_key" yaml:"api_key"`
URL string `json:"url" toml:"url" xml:"url" yaml:"url"` URL string `json:"url" toml:"url" xml:"url" yaml:"url"`
Sites []string `json:"sites" toml:"sites" xml:"site" yaml:"sites"` Sites []string `json:"sites" toml:"sites" xml:"site" yaml:"sites"`
Unifi *unifi.Unifi `json:"-" toml:"-" xml:"-" yaml:"-"` DefaultSiteNameOverride string `json:"default_site_name_override" toml:"default_site_name_override" xml:"default_site_name_override" yaml:"default_site_name_override"`
ID string `json:"id,omitempty"` // this is an output, not an input. Unifi *unifi.Unifi `json:"-" toml:"-" xml:"-" yaml:"-"`
ID string `json:"id,omitempty"` // this is an output, not an input.
} }
// Config contains our configuration data. // Config contains our configuration data.
@ -358,6 +359,11 @@ func (u *InputUnifi) setControllerDefaults(c *Controller) *Controller { //nolint
c.Sites = u.Default.Sites c.Sites = u.Default.Sites
} }
// Added handling for the new default_site_name_override parameter.
if c.DefaultSiteNameOverride == "" {
c.DefaultSiteNameOverride = u.Default.DefaultSiteNameOverride
}
return c return c
} }