@@ -39,6 +39,7 @@ func AuditLogs(ctx context.Context, db database.Store, query string) (database.G
39
39
Email :parser .String (values ,"" ,"email" ),
40
40
DateFrom :parser .Time (values , time.Time {},"date_from" ,dateLayout ),
41
41
DateTo :parser .Time (values , time.Time {},"date_to" ,dateLayout ),
42
+ OrganizationID :parseOrganization (ctx ,db ,parser ,values ,"organization" ),
42
43
ResourceType :string (httpapi .ParseCustom (parser ,values ,"" ,"resource_type" ,httpapi .ParseEnum [database .ResourceType ])),
43
44
Action :string (httpapi .ParseCustom (parser ,values ,"" ,"action" ,httpapi .ParseEnum [database .AuditAction ])),
44
45
BuildReason :string (httpapi .ParseCustom (parser ,values ,"" ,"build_reason" ,httpapi .ParseEnum [database .BuildReason ])),
@@ -47,27 +48,6 @@ func AuditLogs(ctx context.Context, db database.Store, query string) (database.G
47
48
filter .DateTo = filter .DateTo .Add (23 * time .Hour + 59 * time .Minute + 59 * time .Second )
48
49
}
49
50
50
- // Convert the "organization" parameter to an organization uuid. This can require
51
- // a database lookup.
52
- organizationArg := parser .String (values ,"" ,"organization" )
53
- if organizationArg != "" {
54
- organizationID ,err := uuid .Parse (organizationArg )
55
- if err == nil {
56
- filter .OrganizationID = organizationID
57
- }else {
58
- // Organization could be a name
59
- organization ,err := db .GetOrganizationByName (ctx ,organizationArg )
60
- if err != nil {
61
- parser .Errors = append (parser .Errors , codersdk.ValidationError {
62
- Field :"organization" ,
63
- Detail :fmt .Sprintf ("Organization %q either does not exist, or you are unauthorized to view it" ,organizationArg ),
64
- })
65
- }else {
66
- filter .OrganizationID = organization .ID
67
- }
68
- }
69
- }
70
-
71
51
parser .ErrorExcessParams (values )
72
52
return filter ,parser .Errors
73
53
}
@@ -95,7 +75,7 @@ func Users(query string) (database.GetUsersParams, []codersdk.ValidationError) {
95
75
return filter ,parser .Errors
96
76
}
97
77
98
- func Workspaces (query string ,page codersdk.Pagination ,agentInactiveDisconnectTimeout time.Duration ) (database.GetWorkspacesParams , []codersdk.ValidationError ) {
78
+ func Workspaces (ctx context. Context , db database. Store , query string ,page codersdk.Pagination ,agentInactiveDisconnectTimeout time.Duration ) (database.GetWorkspacesParams , []codersdk.ValidationError ) {
99
79
filter := database.GetWorkspacesParams {
100
80
AgentInactiveDisconnectTimeoutSeconds :int64 (agentInactiveDisconnectTimeout .Seconds ()),
101
81
@@ -145,6 +125,7 @@ func Workspaces(query string, page codersdk.Pagination, agentInactiveDisconnectT
145
125
// which will return all workspaces.
146
126
Valid :values .Has ("outdated" ),
147
127
}
128
+ filter .OrganizationID = parseOrganization (ctx ,db ,parser ,values ,"organization" )
148
129
149
130
type paramMatch struct {
150
131
name string
@@ -198,32 +179,12 @@ func Templates(ctx context.Context, db database.Store, query string) (database.G
198
179
199
180
parser := httpapi .NewQueryParamParser ()
200
181
filter := database.GetTemplatesWithFilterParams {
201
- Deleted :parser .Boolean (values ,false ,"deleted" ),
202
- ExactName :parser .String (values ,"" ,"exact_name" ),
203
- FuzzyName :parser .String (values ,"" ,"name" ),
204
- IDs :parser .UUIDs (values , []uuid.UUID {},"ids" ),
205
- Deprecated :parser .NullableBoolean (values , sql.NullBool {},"deprecated" ),
206
- }
207
-
208
- // Convert the "organization" parameter to an organization uuid. This can require
209
- // a database lookup.
210
- organizationArg := parser .String (values ,"" ,"organization" )
211
- if organizationArg != "" {
212
- organizationID ,err := uuid .Parse (organizationArg )
213
- if err == nil {
214
- filter .OrganizationID = organizationID
215
- }else {
216
- // Organization could be a name
217
- organization ,err := db .GetOrganizationByName (ctx ,organizationArg )
218
- if err != nil {
219
- parser .Errors = append (parser .Errors , codersdk.ValidationError {
220
- Field :"organization" ,
221
- Detail :fmt .Sprintf ("Organization %q either does not exist, or you are unauthorized to view it" ,organizationArg ),
222
- })
223
- }else {
224
- filter .OrganizationID = organization .ID
225
- }
226
- }
182
+ Deleted :parser .Boolean (values ,false ,"deleted" ),
183
+ ExactName :parser .String (values ,"" ,"exact_name" ),
184
+ FuzzyName :parser .String (values ,"" ,"name" ),
185
+ IDs :parser .UUIDs (values , []uuid.UUID {},"ids" ),
186
+ Deprecated :parser .NullableBoolean (values , sql.NullBool {},"deprecated" ),
187
+ OrganizationID :parseOrganization (ctx ,db ,parser ,values ,"organization" ),
227
188
}
228
189
229
190
parser .ErrorExcessParams (values )
@@ -271,6 +232,23 @@ func searchTerms(query string, defaultKey func(term string, values url.Values) e
271
232
return searchValues ,nil
272
233
}
273
234
235
+ func parseOrganization (ctx context.Context ,db database.Store ,parser * httpapi.QueryParamParser ,vals url.Values ,queryParam string ) uuid.UUID {
236
+ return httpapi .ParseCustom (parser ,vals ,uuid .Nil ,queryParam ,func (v string ) (uuid.UUID ,error ) {
237
+ if v == "" {
238
+ return uuid .Nil ,nil
239
+ }
240
+ organizationID ,err := uuid .Parse (v )
241
+ if err == nil {
242
+ return organizationID ,nil
243
+ }
244
+ organization ,err := db .GetOrganizationByName (ctx ,v )
245
+ if err != nil {
246
+ return uuid .Nil ,xerrors .Errorf ("organization %q either does not exist, or you are unauthorized to view it" ,v )
247
+ }
248
+ return organization .ID ,nil
249
+ })
250
+ }
251
+
274
252
// splitQueryParameterByDelimiter takes a query string and splits it into the individual elements
275
253
// of the query. Each element is separated by a delimiter. All quoted strings are
276
254
// kept as a single element.