|
| 1 | +package gapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | +) |
| 7 | + |
| 8 | +type ResourcePermission struct { |
| 9 | + ID int64 `json:"id"` |
| 10 | + RoleName string `json:"roleName"` |
| 11 | + IsManaged bool `json:"isManaged"` |
| 12 | + IsInherited bool `json:"isInherited"` |
| 13 | + IsServiceAccount bool `json:"isServiceAccount"` |
| 14 | + UserID int64 `json:"userId,omitempty"` |
| 15 | + UserLogin string `json:"userLogin,omitempty"` |
| 16 | + UserAvatarURL string `json:"userAvatarUrl,omitempty"` |
| 17 | + Team string `json:"team,omitempty"` |
| 18 | + TeamID int64 `json:"teamId,omitempty"` |
| 19 | + TeamAvatarUrl string `json:"teamAvatarUrl,omitempty"` |
| 20 | + BuiltInRole string `json:"builtInRole,omitempty"` |
| 21 | + Actions []string `json:"actions"` |
| 22 | + Permission string `json:"permission"` |
| 23 | +} |
| 24 | + |
| 25 | +type SetResourcePermissionsBody struct { |
| 26 | + Permissions []SetResourcePermissionItem `json:"permissions"` |
| 27 | +} |
| 28 | + |
| 29 | +type SetResourcePermissionBody struct { |
| 30 | + Permission SetResourcePermissionItem `json:"permission"` |
| 31 | +} |
| 32 | + |
| 33 | +type SetResourcePermissionItem struct { |
| 34 | + UserID int64 `json:"userId,omitempty"` |
| 35 | + TeamID int64 `json:"teamId,omitempty"` |
| 36 | + BuiltinRole string `json:"builtInRole,omitempty"` |
| 37 | + Permission string `json:"permission"` |
| 38 | +} |
| 39 | + |
| 40 | +type SetResourcePermissionsResponse struct { |
| 41 | + Message string `json:"message"` |
| 42 | +} |
| 43 | + |
| 44 | +func (c *Client) listResourcePermissions(resource string, ident ResourceIdent) ([]*ResourcePermission, error) { |
| 45 | + path := fmt.Sprintf("/api/access-control/%s/%s", resource, ident.String()) |
| 46 | + result := make([]*ResourcePermission, 0) |
| 47 | + if err := c.request("GET", path, nil, nil, &result); err != nil { |
| 48 | + return nil, fmt.Errorf("error getting %s resource permissions at %s: %w", resource, path, err) |
| 49 | + } |
| 50 | + return result, nil |
| 51 | +} |
| 52 | + |
| 53 | +func (c *Client) setResourcePermissions(resource string, ident ResourceIdent, body SetResourcePermissionsBody) (*SetResourcePermissionsResponse, error) { |
| 54 | + path := fmt.Sprintf("/api/access-control/%s/%s", resource, ident.String()) |
| 55 | + data, err := json.Marshal(body) |
| 56 | + if err != nil { |
| 57 | + return nil, fmt.Errorf("marshal err: %w", err) |
| 58 | + } |
| 59 | + |
| 60 | + result := SetResourcePermissionsResponse{} |
| 61 | + if err := c.request("POST", path, nil, data, &result); err != nil { |
| 62 | + return nil, fmt.Errorf("error setting %s resource permissions at %s: %w", resource, path, err) |
| 63 | + } |
| 64 | + return &result, nil |
| 65 | +} |
| 66 | + |
| 67 | +func (c *Client) setResourcePermissionByAssignment( |
| 68 | + resource string, |
| 69 | + ident ResourceIdent, |
| 70 | + assignmentKind string, |
| 71 | + assignmentIdent ResourceIdent, |
| 72 | + body SetResourcePermissionBody, |
| 73 | +) (*SetResourcePermissionsResponse, error) { |
| 74 | + path := fmt.Sprintf("/api/access-control/%s/%s/%s/%s", resource, ident.String(), assignmentKind, assignmentIdent.String()) |
| 75 | + data, err := json.Marshal(body) |
| 76 | + if err != nil { |
| 77 | + return nil, fmt.Errorf("marshal err: %w", err) |
| 78 | + } |
| 79 | + |
| 80 | + result := SetResourcePermissionsResponse{} |
| 81 | + if err := c.request("POST", path, nil, data, &result); err != nil { |
| 82 | + return nil, fmt.Errorf("error setting %s resource permissions for %s at %s: %w", resource, assignmentKind, path, err) |
| 83 | + } |
| 84 | + return &result, nil |
| 85 | +} |
0 commit comments