Merge pull request #2 from unifi-poller/dn2_no_role
Deprecate role param.
This commit is contained in:
		
						commit
						4dc85d25cd
					
				| 
						 | 
					@ -35,7 +35,6 @@ func (u *InputUnifi) newDynamicCntrlr(url string) (bool, *Controller) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ccopy := u.Default // copy defaults into new controller
 | 
						ccopy := u.Default // copy defaults into new controller
 | 
				
			||||||
	u.dynamic[url] = &ccopy
 | 
						u.dynamic[url] = &ccopy
 | 
				
			||||||
	u.dynamic[url].Role = url
 | 
					 | 
				
			||||||
	u.dynamic[url].URL = url
 | 
						u.dynamic[url].URL = url
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return true, u.dynamic[url]
 | 
						return true, u.dynamic[url]
 | 
				
			||||||
| 
						 | 
					@ -67,7 +66,7 @@ func (u *InputUnifi) collectController(c *Controller) (*poller.Metrics, error) {
 | 
				
			||||||
		u.Logf("Re-authenticating to UniFi Controller: %s", c.URL)
 | 
							u.Logf("Re-authenticating to UniFi Controller: %s", c.URL)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if err := u.getUnifi(c); err != nil {
 | 
							if err := u.getUnifi(c); err != nil {
 | 
				
			||||||
			return nil, errors.Wrapf(err, "re-authenticating to %s", c.Role)
 | 
								return nil, errors.Wrapf(err, "re-authenticating to %s", c.URL)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -76,7 +75,7 @@ func (u *InputUnifi) collectController(c *Controller) (*poller.Metrics, error) {
 | 
				
			||||||
		u.Logf("Re-authenticating to UniFi Controller: %s", c.URL)
 | 
							u.Logf("Re-authenticating to UniFi Controller: %s", c.URL)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if err := u.getUnifi(c); err != nil {
 | 
							if err := u.getUnifi(c); err != nil {
 | 
				
			||||||
			return metrics, errors.Wrapf(err, "re-authenticating to %s", c.Role)
 | 
								return metrics, errors.Wrapf(err, "re-authenticating to %s", c.URL)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -38,7 +38,6 @@ type Controller struct {
 | 
				
			||||||
	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"`
 | 
				
			||||||
	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"`
 | 
				
			||||||
	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"`
 | 
				
			||||||
	Role      string       `json:"role" toml:"role" xml:"role,attr" yaml:"role"`
 | 
					 | 
				
			||||||
	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"`
 | 
				
			||||||
	URL       string       `json:"url" toml:"url" xml:"url" yaml:"url"`
 | 
						URL       string       `json:"url" toml:"url" xml:"url" yaml:"url"`
 | 
				
			||||||
| 
						 | 
					@ -117,7 +116,7 @@ func (u *InputUnifi) checkSites(c *Controller) error {
 | 
				
			||||||
		msg = append(msg, site.Name+" ("+site.Desc+")")
 | 
							msg = append(msg, site.Name+" ("+site.Desc+")")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	u.Logf("Found %d site(s) on controller %s: %v", len(msg), c.Role, strings.Join(msg, ", "))
 | 
						u.Logf("Found %d site(s) on controller %s: %v", len(msg), c.URL, strings.Join(msg, ", "))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if StringInSlice("all", c.Sites) {
 | 
						if StringInSlice("all", c.Sites) {
 | 
				
			||||||
		c.Sites = []string{"all"}
 | 
							c.Sites = []string{"all"}
 | 
				
			||||||
| 
						 | 
					@ -134,7 +133,7 @@ FIRST:
 | 
				
			||||||
				continue FIRST
 | 
									continue FIRST
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		u.LogErrorf("Configured site not found on controller %s: %v", c.Role, s)
 | 
							u.LogErrorf("Configured site not found on controller %s: %v", c.URL, s)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if c.Sites = keep; len(keep) == 0 {
 | 
						if c.Sites = keep; len(keep) == 0 {
 | 
				
			||||||
| 
						 | 
					@ -201,10 +200,6 @@ func (u *InputUnifi) setDefaults(c *Controller) {
 | 
				
			||||||
		c.URL = defaultURL
 | 
							c.URL = defaultURL
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if c.Role == "" {
 | 
					 | 
				
			||||||
		c.Role = c.URL
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if strings.HasPrefix(c.Pass, "file://") {
 | 
						if strings.HasPrefix(c.Pass, "file://") {
 | 
				
			||||||
		c.Pass = u.getPassFromFile(strings.TrimPrefix(c.Pass, "file://"))
 | 
							c.Pass = u.getPassFromFile(strings.TrimPrefix(c.Pass, "file://"))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					@ -250,12 +245,6 @@ func (u *InputUnifi) setControllerDefaults(c *Controller) *Controller {
 | 
				
			||||||
		c.URL = u.Default.URL
 | 
							c.URL = u.Default.URL
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if c.Role == "" && u.Default.Role != u.Default.URL {
 | 
					 | 
				
			||||||
		c.Role = u.Default.Role
 | 
					 | 
				
			||||||
	} else if c.Role == "" {
 | 
					 | 
				
			||||||
		c.Role = c.URL
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if strings.HasPrefix(c.Pass, "file://") {
 | 
						if strings.HasPrefix(c.Pass, "file://") {
 | 
				
			||||||
		c.Pass = u.getPassFromFile(strings.TrimPrefix(c.Pass, "file://"))
 | 
							c.Pass = u.getPassFromFile(strings.TrimPrefix(c.Pass, "file://"))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -45,7 +45,7 @@ func (u *InputUnifi) Initialize(l poller.Logger) error {
 | 
				
			||||||
		switch err := u.getUnifi(u.setControllerDefaults(c)); err {
 | 
							switch err := u.getUnifi(u.setControllerDefaults(c)); err {
 | 
				
			||||||
		case nil:
 | 
							case nil:
 | 
				
			||||||
			if err := u.checkSites(c); err != nil {
 | 
								if err := u.checkSites(c); err != nil {
 | 
				
			||||||
				u.LogErrorf("checking sites on %s: %v", c.Role, err)
 | 
									u.LogErrorf("checking sites on %s: %v", c.URL, err)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			u.Logf("Configured UniFi Controller %d:", i+1)
 | 
								u.Logf("Configured UniFi Controller %d:", i+1)
 | 
				
			||||||
| 
						 | 
					@ -67,7 +67,6 @@ func (u *InputUnifi) logController(c *Controller) {
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	u.Logf("   => Username: %s (has password: %v)", c.User, c.Pass != "")
 | 
						u.Logf("   => Username: %s (has password: %v)", c.User, c.Pass != "")
 | 
				
			||||||
	u.Logf("   => Role: %s", c.Role)
 | 
					 | 
				
			||||||
	u.Logf("   => Hash PII: %v", *c.HashPII)
 | 
						u.Logf("   => Hash PII: %v", *c.HashPII)
 | 
				
			||||||
	u.Logf("   => Verify SSL: %v", *c.VerifySSL)
 | 
						u.Logf("   => Verify SSL: %v", *c.VerifySSL)
 | 
				
			||||||
	u.Logf("   => Save DPI: %v", *c.SaveDPI)
 | 
						u.Logf("   => Save DPI: %v", *c.SaveDPI)
 | 
				
			||||||
| 
						 | 
					@ -86,45 +85,34 @@ func (u *InputUnifi) MetricsFrom(filter *poller.Filter) (*poller.Metrics, bool,
 | 
				
			||||||
		return nil, false, nil
 | 
							return nil, false, nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	errs := []string{}
 | 
					 | 
				
			||||||
	metrics := &poller.Metrics{}
 | 
						metrics := &poller.Metrics{}
 | 
				
			||||||
	ok := false
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if filter != nil && filter.Path != "" {
 | 
						// Check if the request is for an existing, configured controller (or all controllers)
 | 
				
			||||||
 | 
						for _, c := range u.Controllers {
 | 
				
			||||||
 | 
							if filter != nil && !strings.EqualFold(c.URL, filter.Path) {
 | 
				
			||||||
 | 
								continue
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							m, err := u.collectController(c)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return metrics, false, err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							metrics = poller.AppendMetrics(metrics, m)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if filter == nil || len(metrics.Clients) != 0 {
 | 
				
			||||||
 | 
							return metrics, true, nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if !u.Dynamic {
 | 
						if !u.Dynamic {
 | 
				
			||||||
			return metrics, false, errDynamicLookupsDisabled
 | 
							return nil, false, errDynamicLookupsDisabled
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Attempt a dynamic metrics fetch from an unconfigured controller.
 | 
						// Attempt a dynamic metrics fetch from an unconfigured controller.
 | 
				
			||||||
	m, err := u.dynamicController(filter.Path)
 | 
						m, err := u.dynamicController(filter.Path)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return m, err == nil && m != nil, err
 | 
						return m, err == nil && m != nil, err
 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// Check if the request is for an existing, configured controller.
 | 
					 | 
				
			||||||
	for _, c := range u.Controllers {
 | 
					 | 
				
			||||||
		if filter != nil && !strings.EqualFold(c.Role, filter.Role) {
 | 
					 | 
				
			||||||
			continue
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		m, err := u.collectController(c)
 | 
					 | 
				
			||||||
		if err != nil {
 | 
					 | 
				
			||||||
			errs = append(errs, err.Error())
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		if m == nil {
 | 
					 | 
				
			||||||
			continue
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		ok = true
 | 
					 | 
				
			||||||
		metrics = poller.AppendMetrics(metrics, m)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if len(errs) > 0 {
 | 
					 | 
				
			||||||
		return metrics, ok, fmt.Errorf(strings.Join(errs, ", ")) // nolint: goerr113
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return metrics, ok, nil
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// RawMetrics returns API output from the first configured unifi controller.
 | 
					// RawMetrics returns API output from the first configured unifi controller.
 | 
				
			||||||
| 
						 | 
					@ -138,7 +126,7 @@ func (u *InputUnifi) RawMetrics(filter *poller.Filter) ([]byte, error) {
 | 
				
			||||||
		u.Logf("Re-authenticating to UniFi Controller: %s", c.URL)
 | 
							u.Logf("Re-authenticating to UniFi Controller: %s", c.URL)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if err := u.getUnifi(c); err != nil {
 | 
							if err := u.getUnifi(c); err != nil {
 | 
				
			||||||
			return nil, errors.Wrapf(err, "re-authenticating to %s", c.Role)
 | 
								return nil, errors.Wrapf(err, "re-authenticating to %s", c.URL)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue