feat(01-03): add location hierarchy provisioning and provision CLI script
- ProvisionLocationHierarchy: idempotent Site/Location/Rack creation - ensureSite/ensureLocation/ensureRack using go-netbox v4 typed requests - CheckNetBoxInventoryPlugin via /api/plugins/ endpoint - scripts/provision-netbox.go CLI with go:build ignore tag
This commit is contained in:
parent
e07ad922eb
commit
49a729a1a6
1 changed files with 57 additions and 0 deletions
57
scripts/provision-netbox.go
Normal file
57
scripts/provision-netbox.go
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
//go:build ignore
|
||||
|
||||
// Run with: go run scripts/provision-netbox.go
|
||||
// Provisions NetBox with all HWLab custom fields and location hierarchy.
|
||||
// Reads HWLAB_NETBOX_URL and HWLAB_NETBOX_TOKEN from environment (.env auto-loaded).
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
|
||||
"git.georgsen.dk/hwlab/internal/netbox"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Load .env
|
||||
if err := godotenv.Load(); err != nil {
|
||||
log.Printf("no .env file: %v", err)
|
||||
}
|
||||
|
||||
url := os.Getenv("HWLAB_NETBOX_URL")
|
||||
token := os.Getenv("HWLAB_NETBOX_TOKEN")
|
||||
|
||||
if url == "" || token == "" {
|
||||
log.Fatal("HWLAB_NETBOX_URL and HWLAB_NETBOX_TOKEN must be set")
|
||||
}
|
||||
|
||||
client, err := netbox.NewClient(url, token)
|
||||
if err != nil {
|
||||
log.Fatalf("netbox client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
log.Println("Provisioning NetBox...")
|
||||
if err := client.Provision(ctx); err != nil {
|
||||
log.Fatalf("provision: %v", err)
|
||||
}
|
||||
|
||||
// Check netbox-inventory plugin (NB-03)
|
||||
ok, err := client.CheckNetBoxInventoryPlugin(ctx)
|
||||
if err != nil {
|
||||
log.Printf("plugin check error: %v", err)
|
||||
} else if ok {
|
||||
log.Println("netbox-inventory plugin: INSTALLED")
|
||||
} else {
|
||||
log.Println("WARNING: netbox-inventory plugin may not be installed")
|
||||
log.Println(" Manual check: SSH to LXC 130, run: pip show netbox-inventory")
|
||||
log.Println(" Install if missing: pip install netbox-inventory")
|
||||
}
|
||||
|
||||
log.Println("Provisioning complete.")
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue