Theo pushed to branch main at Root / CLI / Netboxadm
Commits:
-
e352c093
by Theo at 2025-08-06T13:15:31-03:00
3 changed files:
Changes:
... | ... | @@ -77,10 +77,29 @@ func init() { |
77 | 77 | AssignCmd.Flags().StringVarP(&AssignCfg.Groups, "setup-hosts-groups", "g", "", "Comma-separated list of groups for setup-hosts (eg. sudo)")
|
78 | 78 | AssignCmd.Flags().StringVarP(&AssignCfg.ContactName, "name", "n", "", "Contact full exact name")
|
79 | 79 | AssignCmd.Flags().StringVarP(&AssignCfg.Priority, "priority", "p", "", "Assignment priority")
|
80 | + AssignCmd.Flags().StringVarP(&AssignCfg.RoleString, "role", "r", "", "Netbox contact role slug (insensitive contains)")
|
|
80 | 81 | AssignCmd.MarkFlagsRequiredTogether("object-type", "object-id")
|
81 | 82 | }
|
82 | 83 | |
83 | 84 | func HandleDirectInputAssign(cfg *contact.ContactAssignConfig, cmd *cobra.Command, client *rest.Client) error {
|
85 | + |
|
86 | + if cfg.RoleName != "" {
|
|
87 | + query := rest.NewQuery()
|
|
88 | + query.Filter("slug", rest.IC, cfg.RoleName).Brief()
|
|
89 | + |
|
90 | + res, err := rest.GET[models.ContactRoleBrief](client, r.ContactRoles, query)
|
|
91 | + if err != nil {
|
|
92 | + return err
|
|
93 | + }
|
|
94 | + |
|
95 | + if len(res) != 1 {
|
|
96 | + return fmt.Errorf("expected 1 contact role but matched %d", len(res))
|
|
97 | + }
|
|
98 | + |
|
99 | + cfg.RoleName = res[0].Name
|
|
100 | + cfg.RoleSlug = res[0].Slug
|
|
101 | + }
|
|
102 | + |
|
84 | 103 | if cfg.ContactName != "" {
|
85 | 104 | query := rest.NewQuery()
|
86 | 105 | query.Filter("name", rest.EX, cfg.ContactName).Fields("name", "id")
|
... | ... | @@ -172,5 +191,15 @@ func GetMissingFieldsAssign(cfg *contact.ContactAssignConfig, cmd *cobra.Command |
172 | 191 | cfg.Groups = res
|
173 | 192 | }
|
174 | 193 | |
194 | + if cfg.RoleString == "" || cfg.RoleName == "" || cfg.RoleSlug == "" {
|
|
195 | + res, err := nethuh.SelectContactRoleBrief(client)
|
|
196 | + if err != nil {
|
|
197 | + return err
|
|
198 | + }
|
|
199 | + |
|
200 | + cfg.RoleName = res.Name
|
|
201 | + cfg.RoleSlug = res.Slug
|
|
202 | + }
|
|
203 | + |
|
175 | 204 | return nil
|
176 | 205 | } |
... | ... | @@ -66,6 +66,35 @@ func SelectDeviceType(client *rest.Client) (*models.DeviceType, error) { |
66 | 66 | return selected, err
|
67 | 67 | }
|
68 | 68 | |
69 | +func SelectContactRoleBrief(client *rest.Client) (*models.ContactRoleBrief, error) {
|
|
70 | + q := rest.NewQuery().Brief()
|
|
71 | + res, err := rest.GET[models.ContactRoleBrief](client, route.ContactRoles, q)
|
|
72 | + if err != nil {
|
|
73 | + return nil, err
|
|
74 | + }
|
|
75 | + |
|
76 | + if len(res) == 0 {
|
|
77 | + return nil, errors.New("no device types found")
|
|
78 | + }
|
|
79 | + |
|
80 | + options := []huh.Option[*models.ContactRoleBrief]{}
|
|
81 | + |
|
82 | + for _, item := range res {
|
|
83 | + options = append(options, huh.NewOption(item.Display, &item))
|
|
84 | + }
|
|
85 | + |
|
86 | + var selected *models.ContactRoleBrief
|
|
87 | + |
|
88 | + err = huh.NewSelect[*models.ContactRoleBrief]().
|
|
89 | + Title("Select Contact Role").
|
|
90 | + Options(options...).
|
|
91 | + Value(&selected).
|
|
92 | + WithHeight(6).
|
|
93 | + Run()
|
|
94 | + |
|
95 | + return selected, err
|
|
96 | +}
|
|
97 | + |
|
69 | 98 | func SelectSite(client *rest.Client) (*models.Site, error) {
|
70 | 99 | res, err := rest.GET[models.Site](client, route.Sites, nil)
|
71 | 100 | if err != nil {
|
... | ... | @@ -36,14 +36,12 @@ type ContactAssignConfig struct { |
36 | 36 | ObjectID int
|
37 | 37 | Priority string
|
38 | 38 | Groups string
|
39 | + RoleString string
|
|
40 | + RoleSlug string
|
|
41 | + RoleName string
|
|
39 | 42 | }
|
40 | 43 | |
41 | 44 | func (cfg *ContactAssignConfig) Assign(client *rest.Client) (*models.ContactAssignment, error) {
|
42 | - role := models.ContactRoleBrief{
|
|
43 | - Name: "Usuário", // hardcoded cause there is only one for now
|
|
44 | - Slug: "usurio", // and i dont want to annoy the user...
|
|
45 | - }
|
|
46 | - |
|
47 | 45 | typeStr := "dcim.device"
|
48 | 46 | if cfg.ObjectType == models.ObjectTypeVM {
|
49 | 47 | typeStr = "virtualization.virtualmachine"
|
... | ... | @@ -53,8 +51,11 @@ func (cfg *ContactAssignConfig) Assign(client *rest.Client) (*models.ContactAssi |
53 | 51 | Contact: cfg.ContactID,
|
54 | 52 | ObjectType: typeStr,
|
55 | 53 | ObjectID: cfg.ObjectID,
|
56 | - Role: role,
|
|
57 | 54 | Priority: cfg.Priority,
|
55 | + Role: models.ContactRoleBrief{
|
|
56 | + Name: cfg.RoleName,
|
|
57 | + Slug: cfg.RoleSlug,
|
|
58 | + },
|
|
58 | 59 | CustomFields: struct {
|
59 | 60 | Groups string `json:"setup_hosts_additional_groups"`
|
60 | 61 | }{cfg.Groups},
|
... | ... | @@ -67,11 +68,12 @@ func (cfg *ContactAssignConfig) String() string { |
67 | 68 | str := "New Assignment:\n"
|
68 | 69 | |
69 | 70 | tyype := "dev"
|
70 | - if cfg.ObjectType != "dcim.device" {
|
|
71 | + if cfg.ObjectType != models.ObjectTypeDevice {
|
|
71 | 72 | tyype = "vm"
|
72 | 73 | }
|
73 | 74 | |
74 | 75 | str += fmt.Sprintf(" %v --> %v (%v:%v)\n", cfg.ContactName, cfg.ObjectName, tyype, cfg.ObjectID)
|
76 | + str += fmt.Sprintf(" Role: %v\n", cfg.RoleName)
|
|
75 | 77 | if cfg.Groups != "" {
|
76 | 78 | str += fmt.Sprintf(" Groups: %v\n", cfg.Groups)
|
77 | 79 | }
|