[Git][root/cli/netboxadm][main] feat(contact): now allow for role choice uppon contact assignment

Theo pushed to branch main at Root / CLI / Netboxadm Commits: e352c093 by Theo at 2025-08-06T13:15:31-03:00 feat(contact): now allow for role choice uppon contact assignment - - - - - 3 changed files: - cmd/contact/assign.go - internal/nethuh/prompts.go - internal/types/contact/assign.go Changes: ===================================== cmd/contact/assign.go ===================================== @@ -77,10 +77,29 @@ func init() { AssignCmd.Flags().StringVarP(&AssignCfg.Groups, "setup-hosts-groups", "g", "", "Comma-separated list of groups for setup-hosts (eg. sudo)") AssignCmd.Flags().StringVarP(&AssignCfg.ContactName, "name", "n", "", "Contact full exact name") AssignCmd.Flags().StringVarP(&AssignCfg.Priority, "priority", "p", "", "Assignment priority") + AssignCmd.Flags().StringVarP(&AssignCfg.RoleString, "role", "r", "", "Netbox contact role slug (insensitive contains)") AssignCmd.MarkFlagsRequiredTogether("object-type", "object-id") } func HandleDirectInputAssign(cfg *contact.ContactAssignConfig, cmd *cobra.Command, client *rest.Client) error { + + if cfg.RoleName != "" { + query := rest.NewQuery() + query.Filter("slug", rest.IC, cfg.RoleName).Brief() + + res, err := rest.GET[models.ContactRoleBrief](client, r.ContactRoles, query) + if err != nil { + return err + } + + if len(res) != 1 { + return fmt.Errorf("expected 1 contact role but matched %d", len(res)) + } + + cfg.RoleName = res[0].Name + cfg.RoleSlug = res[0].Slug + } + if cfg.ContactName != "" { query := rest.NewQuery() query.Filter("name", rest.EX, cfg.ContactName).Fields("name", "id") @@ -172,5 +191,15 @@ func GetMissingFieldsAssign(cfg *contact.ContactAssignConfig, cmd *cobra.Command cfg.Groups = res } + if cfg.RoleString == "" || cfg.RoleName == "" || cfg.RoleSlug == "" { + res, err := nethuh.SelectContactRoleBrief(client) + if err != nil { + return err + } + + cfg.RoleName = res.Name + cfg.RoleSlug = res.Slug + } + return nil } ===================================== internal/nethuh/prompts.go ===================================== @@ -66,6 +66,35 @@ func SelectDeviceType(client *rest.Client) (*models.DeviceType, error) { return selected, err } +func SelectContactRoleBrief(client *rest.Client) (*models.ContactRoleBrief, error) { + q := rest.NewQuery().Brief() + res, err := rest.GET[models.ContactRoleBrief](client, route.ContactRoles, q) + if err != nil { + return nil, err + } + + if len(res) == 0 { + return nil, errors.New("no device types found") + } + + options := []huh.Option[*models.ContactRoleBrief]{} + + for _, item := range res { + options = append(options, huh.NewOption(item.Display, &item)) + } + + var selected *models.ContactRoleBrief + + err = huh.NewSelect[*models.ContactRoleBrief](). + Title("Select Contact Role"). + Options(options...). + Value(&selected). + WithHeight(6). + Run() + + return selected, err +} + func SelectSite(client *rest.Client) (*models.Site, error) { res, err := rest.GET[models.Site](client, route.Sites, nil) if err != nil { ===================================== internal/types/contact/assign.go ===================================== @@ -36,14 +36,12 @@ type ContactAssignConfig struct { ObjectID int Priority string Groups string + RoleString string + RoleSlug string + RoleName string } func (cfg *ContactAssignConfig) Assign(client *rest.Client) (*models.ContactAssignment, error) { - role := models.ContactRoleBrief{ - Name: "Usuário", // hardcoded cause there is only one for now - Slug: "usurio", // and i dont want to annoy the user... - } - typeStr := "dcim.device" if cfg.ObjectType == models.ObjectTypeVM { typeStr = "virtualization.virtualmachine" @@ -53,8 +51,11 @@ func (cfg *ContactAssignConfig) Assign(client *rest.Client) (*models.ContactAssi Contact: cfg.ContactID, ObjectType: typeStr, ObjectID: cfg.ObjectID, - Role: role, Priority: cfg.Priority, + Role: models.ContactRoleBrief{ + Name: cfg.RoleName, + Slug: cfg.RoleSlug, + }, CustomFields: struct { Groups string `json:"setup_hosts_additional_groups"` }{cfg.Groups}, @@ -67,11 +68,12 @@ func (cfg *ContactAssignConfig) String() string { str := "New Assignment:\n" tyype := "dev" - if cfg.ObjectType != "dcim.device" { + if cfg.ObjectType != models.ObjectTypeDevice { tyype = "vm" } str += fmt.Sprintf(" %v --> %v (%v:%v)\n", cfg.ContactName, cfg.ObjectName, tyype, cfg.ObjectID) + str += fmt.Sprintf(" Role: %v\n", cfg.RoleName) if cfg.Groups != "" { str += fmt.Sprintf(" Groups: %v\n", cfg.Groups) } View it on GitLab: https://gitlab.c3sl.ufpr.br/root/cli/netboxadm/-/commit/e352c093209ee09e9e7b... -- View it on GitLab: https://gitlab.c3sl.ufpr.br/root/cli/netboxadm/-/commit/e352c093209ee09e9e7b... You're receiving this email because of your account on gitlab.c3sl.ufpr.br.
participantes (1)
-
Theo (@tss24)