feat: search multiple NetBox instances with isolated host identities
Release / release (push) Successful in 5m16s
Release / release (push) Successful in 5m16s
This commit is contained in:
1 parent
a2a6dd7591
commit
7f53b90266
26 files changed
+1299
-115
No files matched your search
+17
-7
@@ -34,11 +34,11 @@ func notesCmd() *cobra.Command {
|
||||
return err
|
||||
}
|
||||
var client *netbox.Client
|
||||
if cfg.NetBox.URL != "" {
|
||||
client = netbox.NewClient(cfg.NetBox.URL, cfg.NetBox.Token, cfg.NetBox.TokenVersion)
|
||||
if cfg.HasNetBox() {
|
||||
client = newNetBoxClient(cfg)
|
||||
}
|
||||
host := netbox.HostEntry{Name: args[0], Kind: kind}
|
||||
c := cache.New(cfg.Cache.Path, cfg.Cache.TTL)
|
||||
c := cache.New(cfg.CachePath(), cfg.Cache.TTL)
|
||||
if err := c.Load(); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -46,13 +46,23 @@ func notesCmd() *cobra.Command {
|
||||
if !remote {
|
||||
cached = c.All()
|
||||
}
|
||||
query := host.Name
|
||||
source, rawName := netbox.SplitTarget(query)
|
||||
var matches []cache.Entry
|
||||
for _, entry := range cached {
|
||||
if strings.EqualFold(entry.Name, host.Name) && (kind == "" || kind == entry.Kind) {
|
||||
host.Name = entry.Name
|
||||
host.Kind = entry.Kind
|
||||
break
|
||||
_, entryName := netbox.SplitTarget(entry.Name)
|
||||
if (kind == "" || kind == entry.Kind) && (strings.EqualFold(entry.Name, query) || (source == "" && strings.EqualFold(entryName, rawName))) {
|
||||
matches = append(matches, entry)
|
||||
}
|
||||
}
|
||||
if len(matches) > 1 {
|
||||
return fmt.Errorf("ambiguous hostname %q; use instance/hostname", query)
|
||||
}
|
||||
if len(matches) == 1 {
|
||||
host.Name = matches[0].Name
|
||||
host.Kind = matches[0].Kind
|
||||
}
|
||||
|
||||
if !remote && host.Kind == "" {
|
||||
return fmt.Errorf("host is not cached; specify --kind device or --kind vm")
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user