|
| 1 | +package truenas |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | +) |
| 8 | + |
| 9 | +// Group is the user-facing representation of a TrueNAS group. |
| 10 | +type Group struct { |
| 11 | + ID int64 |
| 12 | + GID int64 |
| 13 | + Name string |
| 14 | + Builtin bool |
| 15 | + SMB bool |
| 16 | + SudoCommands []string |
| 17 | + SudoCommandsNopasswd []string |
| 18 | + Users []int64 |
| 19 | + Local bool |
| 20 | + Immutable bool |
| 21 | +} |
| 22 | + |
| 23 | +// CreateGroupOpts contains options for creating a group. |
| 24 | +type CreateGroupOpts struct { |
| 25 | + Name string |
| 26 | + GID int64 // 0 = auto-assign |
| 27 | + SMB bool |
| 28 | + SudoCommands []string |
| 29 | + SudoCommandsNopasswd []string |
| 30 | +} |
| 31 | + |
| 32 | +// UpdateGroupOpts contains options for updating a group. |
| 33 | +// GID is immutable and cannot be changed after creation. |
| 34 | +type UpdateGroupOpts struct { |
| 35 | + Name string |
| 36 | + SMB bool |
| 37 | + SudoCommands []string |
| 38 | + SudoCommandsNopasswd []string |
| 39 | +} |
| 40 | + |
| 41 | +// GroupService provides typed methods for the group.* API namespace. |
| 42 | +type GroupService struct { |
| 43 | + client Caller |
| 44 | + version Version |
| 45 | +} |
| 46 | + |
| 47 | +// NewGroupService creates a new GroupService. |
| 48 | +func NewGroupService(c Caller, v Version) *GroupService { |
| 49 | + return &GroupService{client: c, version: v} |
| 50 | +} |
| 51 | + |
| 52 | +// Create creates a group and returns the full object. |
| 53 | +func (s *GroupService) Create(ctx context.Context, opts CreateGroupOpts) (*Group, error) { |
| 54 | + params := groupCreateOptsToParams(opts) |
| 55 | + result, err := s.client.Call(ctx, "group.create", params) |
| 56 | + if err != nil { |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + |
| 60 | + var id int64 |
| 61 | + if err := json.Unmarshal(result, &id); err != nil { |
| 62 | + return nil, fmt.Errorf("parse create response: %w", err) |
| 63 | + } |
| 64 | + |
| 65 | + return s.Get(ctx, id) |
| 66 | +} |
| 67 | + |
| 68 | +// Get returns a group by ID, or nil if not found. |
| 69 | +func (s *GroupService) Get(ctx context.Context, id int64) (*Group, error) { |
| 70 | + result, err := s.client.Call(ctx, "group.get_instance", id) |
| 71 | + if err != nil { |
| 72 | + if isNotFoundError(err) { |
| 73 | + return nil, nil |
| 74 | + } |
| 75 | + return nil, err |
| 76 | + } |
| 77 | + |
| 78 | + var resp GroupResponse |
| 79 | + if err := json.Unmarshal(result, &resp); err != nil { |
| 80 | + return nil, fmt.Errorf("parse get_instance response: %w", err) |
| 81 | + } |
| 82 | + |
| 83 | + group := groupFromResponse(resp) |
| 84 | + return &group, nil |
| 85 | +} |
| 86 | + |
| 87 | +// GetByName returns a group by name, or nil if not found. |
| 88 | +func (s *GroupService) GetByName(ctx context.Context, name string) (*Group, error) { |
| 89 | + return s.queryOne(ctx, "group", name) |
| 90 | +} |
| 91 | + |
| 92 | +// GetByGID returns a group by GID, or nil if not found. |
| 93 | +func (s *GroupService) GetByGID(ctx context.Context, gid int64) (*Group, error) { |
| 94 | + return s.queryOne(ctx, "gid", gid) |
| 95 | +} |
| 96 | + |
| 97 | +// List returns all groups. |
| 98 | +func (s *GroupService) List(ctx context.Context) ([]Group, error) { |
| 99 | + result, err := s.client.Call(ctx, "group.query", nil) |
| 100 | + if err != nil { |
| 101 | + return nil, err |
| 102 | + } |
| 103 | + |
| 104 | + var responses []GroupResponse |
| 105 | + if err := json.Unmarshal(result, &responses); err != nil { |
| 106 | + return nil, fmt.Errorf("parse query response: %w", err) |
| 107 | + } |
| 108 | + |
| 109 | + groups := make([]Group, len(responses)) |
| 110 | + for i, resp := range responses { |
| 111 | + groups[i] = groupFromResponse(resp) |
| 112 | + } |
| 113 | + return groups, nil |
| 114 | +} |
| 115 | + |
| 116 | +// Update updates a group and returns the full object. |
| 117 | +func (s *GroupService) Update(ctx context.Context, id int64, opts UpdateGroupOpts) (*Group, error) { |
| 118 | + params := groupUpdateOptsToParams(opts) |
| 119 | + _, err := s.client.Call(ctx, "group.update", []any{id, params}) |
| 120 | + if err != nil { |
| 121 | + return nil, err |
| 122 | + } |
| 123 | + |
| 124 | + return s.Get(ctx, id) |
| 125 | +} |
| 126 | + |
| 127 | +// Delete deletes a group by ID. Does not delete member users. |
| 128 | +func (s *GroupService) Delete(ctx context.Context, id int64) error { |
| 129 | + _, err := s.client.Call(ctx, "group.delete", []any{id, map[string]any{"delete_users": false}}) |
| 130 | + return err |
| 131 | +} |
| 132 | + |
| 133 | +// queryOne queries for a single group by field and value. |
| 134 | +func (s *GroupService) queryOne(ctx context.Context, field string, value any) (*Group, error) { |
| 135 | + filter := [][]any{{field, "=", value}} |
| 136 | + result, err := s.client.Call(ctx, "group.query", filter) |
| 137 | + if err != nil { |
| 138 | + return nil, err |
| 139 | + } |
| 140 | + |
| 141 | + var responses []GroupResponse |
| 142 | + if err := json.Unmarshal(result, &responses); err != nil { |
| 143 | + return nil, fmt.Errorf("parse query response: %w", err) |
| 144 | + } |
| 145 | + |
| 146 | + if len(responses) == 0 { |
| 147 | + return nil, nil |
| 148 | + } |
| 149 | + |
| 150 | + group := groupFromResponse(responses[0]) |
| 151 | + return &group, nil |
| 152 | +} |
| 153 | + |
| 154 | +// groupCreateOptsToParams converts CreateGroupOpts to API parameters. |
| 155 | +func groupCreateOptsToParams(opts CreateGroupOpts) map[string]any { |
| 156 | + params := map[string]any{ |
| 157 | + "name": opts.Name, |
| 158 | + "smb": opts.SMB, |
| 159 | + } |
| 160 | + if opts.GID != 0 { |
| 161 | + params["gid"] = opts.GID |
| 162 | + } |
| 163 | + if opts.SudoCommands != nil { |
| 164 | + params["sudo_commands"] = opts.SudoCommands |
| 165 | + } |
| 166 | + if opts.SudoCommandsNopasswd != nil { |
| 167 | + params["sudo_commands_nopasswd"] = opts.SudoCommandsNopasswd |
| 168 | + } |
| 169 | + return params |
| 170 | +} |
| 171 | + |
| 172 | +// groupUpdateOptsToParams converts UpdateGroupOpts to API parameters. |
| 173 | +func groupUpdateOptsToParams(opts UpdateGroupOpts) map[string]any { |
| 174 | + params := map[string]any{ |
| 175 | + "name": opts.Name, |
| 176 | + "smb": opts.SMB, |
| 177 | + } |
| 178 | + if opts.SudoCommands != nil { |
| 179 | + params["sudo_commands"] = opts.SudoCommands |
| 180 | + } |
| 181 | + if opts.SudoCommandsNopasswd != nil { |
| 182 | + params["sudo_commands_nopasswd"] = opts.SudoCommandsNopasswd |
| 183 | + } |
| 184 | + return params |
| 185 | +} |
| 186 | + |
| 187 | +// groupFromResponse converts a wire-format GroupResponse to a user-facing Group. |
| 188 | +func groupFromResponse(resp GroupResponse) Group { |
| 189 | + return Group{ |
| 190 | + ID: resp.ID, |
| 191 | + GID: resp.GID, |
| 192 | + Name: resp.Name, |
| 193 | + Builtin: resp.Builtin, |
| 194 | + SMB: resp.SMB, |
| 195 | + SudoCommands: resp.SudoCommands, |
| 196 | + SudoCommandsNopasswd: resp.SudoCommandsNopasswd, |
| 197 | + Users: resp.Users, |
| 198 | + Local: resp.Local, |
| 199 | + Immutable: resp.Immutable, |
| 200 | + } |
| 201 | +} |
0 commit comments