/
validate_api.go
43 lines (34 loc) · 1.5 KB
/
validate_api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package middleware
// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.
import (
"net/http"
"github.com/go-chi/chi/v5"
"github.com/Azure/ARO-RP/pkg/api"
)
type ApiVersionValidator struct {
APIs map[string]*api.Version
}
func (a ApiVersionValidator) ValidateAPIVersion(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
apiVersion := r.URL.Query().Get(api.APIVersionKey)
resourceType := chi.URLParam(r, "resourceType")
resourceProviderNamespace := chi.URLParam(r, "resourceProviderNamespace")
if _, apiVersionExists := a.APIs[apiVersion]; !apiVersionExists {
api.WriteError(w, http.StatusBadRequest, api.CloudErrorCodeInvalidResourceType, "", "The resource type '%s' could not be found in the namespace '%s' for api version '%s'.", resourceType, resourceProviderNamespace, apiVersion)
return
}
h.ServeHTTP(w, r)
})
}
func (a ApiVersionValidator) ValidatePreflightAPIVersion(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
apiVersion := r.URL.Query().Get(api.APIVersionKey)
resourceProviderNamespace := chi.URLParam(r, "resourceProviderNamespace")
if _, apiVersionExists := a.APIs[apiVersion]; !apiVersionExists {
api.WriteError(w, http.StatusBadRequest, api.CloudErrorCodeInvalidResourceType, "", "Api version %q is invalid for preflight validation in resource provider %q.", apiVersion, resourceProviderNamespace)
return
}
h.ServeHTTP(w, r)
})
}