MCPcopy
hub / github.com/lxc/incus / ObjectFromRequest

Function ObjectFromRequest

internal/server/auth/authorization_objects.go:171–232  ·  view source on GitHub ↗

ObjectFromRequest returns an object created from the request by evaluating the given mux vars. Mux vars must be provided in the order that they are found in the endpoint path. If the object requires a project name, this is taken from the project query parameter unless the URL begins with /1.0/projec

(r *http.Request, objectType ObjectType, expandProject func(string) string, expandFingerprint func(string, string) string, expandVolumeLocation func(string, string, string, string) string, muxVars ...string)

Source from the content-addressed store, hash-verified

169// requires a project name, this is taken from the project query parameter unless the URL begins
170// with /1.0/projects.
171func ObjectFromRequest(r *http.Request, objectType ObjectType, expandProject func(string) string, expandFingerprint func(string, string) string, expandVolumeLocation func(string, string, string, string) string, muxVars ...string) (Object, error) {
172 // Shortcut for server objects which don't require any arguments.
173 if objectType == ObjectTypeServer {
174 return ObjectServer(), nil
175 }
176
177 values, err := url.ParseQuery(r.URL.RawQuery)
178 if err != nil {
179 return "", err
180 }
181
182 projectName := values.Get("project")
183 if projectName == "" {
184 projectName = "default"
185 } else if projectName != "default" {
186 projectName = expandProject(projectName)
187 }
188
189 location := values.Get("target")
190
191 muxValues := make([]string, 0, len(muxVars))
192 for _, muxVar := range muxVars {
193 var muxValue string
194
195 if muxVar == "location" {
196 // Special handling for the location which is not present as a real path variable.
197 if location != "" {
198 muxValue = location
199 } else if objectType == ObjectTypeStorageVolume {
200 muxValue = expandVolumeLocation(projectName, r.PathValue("poolName"), r.PathValue("type"), r.PathValue("volumeName"))
201 }
202
203 if muxValue == "" {
204 continue
205 }
206 } else {
207 // The HTTP router already URL-decodes path segments.
208 muxValue = r.PathValue(muxVar)
209 if muxValue == "" {
210 return "", fmt.Errorf("Mux var %q not found for object type %q", muxVar, objectType)
211 }
212
213 // Expand fingerprints.
214 if muxVar == "fingerprint" {
215 muxValue = expandFingerprint(projectName, muxValue)
216 }
217 }
218
219 muxValues = append(muxValues, muxValue)
220 }
221
222 // If using projects API we want to pass in the mux var, not the query parameter.
223 if objectType == ObjectTypeProject && strings.HasPrefix(r.URL.Path, fmt.Sprintf("/%s/projects", version.APIVersion)) {
224 if len(muxValues) == 0 {
225 return "", errors.New("Missing project name path variable")
226 }
227
228 return ObjectProject(muxValues[0]), nil

Callers 1

allowPermissionFunction · 0.92

Calls 5

ObjectServerFunction · 0.85
ObjectProjectFunction · 0.85
NewObjectFunction · 0.85
ErrorfMethod · 0.80
GetMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…