feat: adding Nautobot Admin Groups and Permission sync - #2157
feat: adding Nautobot Admin Groups and Permission sync#2157abhimanyu003 wants to merge 2 commits into
Conversation
| permissionGroupData map[string]string, | ||
| ) error { | ||
| log := logf.FromContext(ctx) | ||
| log.Info("syncing permission groups", "count", len(permissionGroupData)) |
There was a problem hiding this comment.
I think log statements should be at the end of the code. In case of count zero, since this function is later on calling SyncAll, (len(permissionGroupData)) at the end should be logged.
| "context" | ||
| "net/http" | ||
|
|
||
| "github.com/charmbracelet/log" |
There was a problem hiding this comment.
A suggestion, we should be using "k8s.io/klog/v2". This has more chance of being maintained and up to date with latest vulnerabilities.
Reference Cluster API vSphere
| } | ||
|
|
||
| func (s *GroupService) Create(ctx context.Context, req nb.GroupRequest) (*nb.Group, error) { | ||
| group, resp, err := s.client.APIClient.UsersAPI.UsersGroupsCreate(ctx).GroupRequest(req).Execute() |
There was a problem hiding this comment.
Again an observation! This calls multiple nested functions. We should try to make this more modular.
| group, resp, err := s.client.APIClient.UsersAPI.UsersGroupsCreate(ctx).GroupRequest(req).Execute() | ||
| if err != nil { | ||
| bodyString := helpers.ReadResponseBody(resp) | ||
| s.client.AddReport("createNewGroup", "failed to create", "model", req.Name, "error", err.Error(), "response_body", bodyString) |
| } | ||
|
|
||
| func (s *GroupService) GetByName(ctx context.Context, name string) *nb.Group { | ||
| list, resp, err := s.client.APIClient.UsersAPI.UsersGroupsList(ctx).Name([]string{name}).Execute() |
There was a problem hiding this comment.
Same observation, this call should be more modular.
| } | ||
|
|
||
| func (s *GroupService) ListAll(ctx context.Context) []nb.Group { | ||
| return helpers.PaginatedList( |
There was a problem hiding this comment.
return should not hold a function call.
|
|
||
| // GetOrCreate fetches a group by name or creates it if it doesn't exist. | ||
| func (s *GroupService) GetOrCreate(ctx context.Context, name string) (*nb.Group, error) { | ||
| existing := s.GetByName(ctx, name) |
There was a problem hiding this comment.
existing , err:= s.GetByName(ctx, name). This is more common practice in golang.
| } | ||
|
|
||
| func (s *PermissionService) Create(ctx context.Context, req nb.ObjectPermissionRequest) (*nb.ObjectPermission, error) { | ||
| perm, resp, err := s.client.APIClient.UsersAPI.UsersPermissionsCreate(ctx).ObjectPermissionRequest(req).Execute() |
There was a problem hiding this comment.
I think, we can make this call more modular.
| } | ||
| } | ||
|
|
||
| func (s *PermissionGroupSync) SyncAll(ctx context.Context, data map[string]string) error { |
| s.client.AddReport("yamlFailed", "file: "+key+" error: "+err.Error()) | ||
| return err | ||
| } | ||
| allPermissions = append(allPermissions, config.Permissions...) |
There was a problem hiding this comment.
This piece of code looks really good :)
This PR adds Nautobot Admin Groups and Permission sync.
Example of config map this will sync form.
This is only for admin that's why it's nested into admin folder to keep things cleaner
Groups Screenshot

Permission Screenshot