Parse a group filter value into a group UUID. Supported formats: - - / - (resolved in the default organization)
(ctx context.Context, db database.Store, parser *httpapi.QueryParamParser, vals url.Values, queryParam string)
| 748 | // - <organization-name>/<group-name> |
| 749 | // - <group-name> (resolved in the default organization) |
| 750 | func parseGroup(ctx context.Context, db database.Store, parser *httpapi.QueryParamParser, vals url.Values, queryParam string) uuid.UUID { |
| 751 | return httpapi.ParseCustom(parser, vals, uuid.Nil, queryParam, func(v string) (uuid.UUID, error) { |
| 752 | if v == "" { |
| 753 | return uuid.Nil, nil |
| 754 | } |
| 755 | groupID, err := uuid.Parse(v) |
| 756 | if err == nil { |
| 757 | return groupID, nil |
| 758 | } |
| 759 | |
| 760 | var groupName string |
| 761 | var org database.Organization |
| 762 | parts := strings.Split(v, "/") |
| 763 | switch len(parts) { |
| 764 | case 1: |
| 765 | dbOrg, err := db.GetDefaultOrganization(ctx) |
| 766 | if err != nil { |
| 767 | return uuid.Nil, xerrors.New("fetching default organization") |
| 768 | } |
| 769 | org = dbOrg |
| 770 | groupName = parts[0] |
| 771 | case 2: |
| 772 | orgName := parts[0] |
| 773 | if err := codersdk.NameValid(orgName); err != nil { |
| 774 | return uuid.Nil, xerrors.Errorf("invalid organization name %w", err) |
| 775 | } |
| 776 | dbOrg, err := db.GetOrganizationByName(ctx, database.GetOrganizationByNameParams{ |
| 777 | Name: orgName, |
| 778 | }) |
| 779 | if err != nil { |
| 780 | return uuid.Nil, xerrors.Errorf("organization %q either does not exist, or you are unauthorized to view it", orgName) |
| 781 | } |
| 782 | org = dbOrg |
| 783 | |
| 784 | groupName = parts[1] |
| 785 | |
| 786 | default: |
| 787 | return uuid.Nil, xerrors.New("invalid organization or group name, the filter must be in the pattern of <organization name>/<group name>") |
| 788 | } |
| 789 | |
| 790 | if err := codersdk.GroupNameValid(groupName); err != nil { |
| 791 | return uuid.Nil, xerrors.Errorf("invalid group name %w", err) |
| 792 | } |
| 793 | |
| 794 | group, err := db.GetGroupByOrgAndName(ctx, database.GetGroupByOrgAndNameParams{ |
| 795 | OrganizationID: org.ID, |
| 796 | Name: groupName, |
| 797 | }) |
| 798 | if err != nil { |
| 799 | return uuid.Nil, xerrors.Errorf("group %q either does not exist, does not belong to the organization %q, or you are unauthorized to view it", groupName, org.Name) |
| 800 | } |
| 801 | return group.ID, nil |
| 802 | }) |
| 803 | } |
| 804 | |
| 805 | // splitQueryParameterByDelimiter takes a query string and splits it into the individual elements |
| 806 | // of the query. Each element is separated by a delimiter. All quoted strings are |
no test coverage detected