-
Notifications
You must be signed in to change notification settings - Fork 155
/
request.go
156 lines (126 loc) · 4.13 KB
/
request.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
Copyright 2020 The Kubermatic Kubernetes Platform contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package common
import (
"context"
"fmt"
"net/http"
"github.com/gorilla/mux"
apiv1 "k8c.io/kubermatic/v2/pkg/api/v1"
)
func DecodeEmptyReq(c context.Context, r *http.Request) (interface{}, error) {
var req struct{}
return req, nil
}
// ProjectReq represents a request for project-specific data.
type ProjectReq struct {
// in: path
// required: true
ProjectID string `json:"project_id"`
}
// GetProjectID returns the ID of a requested project.
func (pr ProjectReq) GetProjectID() string {
return pr.ProjectID
}
func DecodeProjectRequest(c context.Context, r *http.Request) (interface{}, error) {
return ProjectReq{
ProjectID: mux.Vars(r)["project_id"],
}, nil
}
// ProjectIDGetter knows how to get project ID from the request.
type ProjectIDGetter interface {
GetProjectID() string
}
// GetProjectRq defines HTTP request for getProject endpoint
// swagger:parameters getProject getUsersForProject listClustersForProject listServiceAccounts getProjectQuota listGroupProjectBinding
type GetProjectRq struct {
ProjectReq
}
func DecodeGetProject(c context.Context, r *http.Request) (interface{}, error) {
projectReq, err := DecodeProjectRequest(c, r)
if err != nil {
return nil, err
}
return GetProjectRq{projectReq.(ProjectReq)}, nil
}
// DCReq represent a request for datacenter specific data in a given project.
type DCReq struct {
ProjectReq
// in: path
// required: true
DC string `json:"dc"`
}
// GetSeedCluster returns the SeedCluster object.
func (req DCReq) GetSeedCluster() apiv1.SeedCluster {
return apiv1.SeedCluster{
SeedName: req.DC,
}
}
func DecodeDcReq(c context.Context, r *http.Request) (interface{}, error) {
projectReq, err := DecodeProjectRequest(c, r)
if err != nil {
return nil, err
}
return DCReq{
DC: mux.Vars(r)["dc"],
ProjectReq: projectReq.(ProjectReq),
}, nil
}
// GetClusterReq defines HTTP request for deleteCluster and getClusterKubeconfig endpoints
// swagger:parameters getCluster getClusterKubeconfig getOidcClusterKubeconfig listAWSSizesNoCredentials getClusterHealth getClusterUpgrades getClusterMetrics getClusterNodeUpgrades listGCPZonesNoCredentials listGCPNetworksNoCredentials listAWSZonesNoCredentials listAWSSubnetsNoCredentials listAlibabaInstanceTypesNoCredentials listNamespace
type GetClusterReq struct {
DCReq
// in: path
// required: true
ClusterID string `json:"cluster_id"`
}
func DecodeGetClusterReq(c context.Context, r *http.Request) (interface{}, error) {
var req GetClusterReq
clusterID, err := DecodeClusterID(c, r)
if err != nil {
return nil, err
}
req.ClusterID = clusterID
dcr, err := DecodeDcReq(c, r)
if err != nil {
return nil, err
}
req.DCReq = dcr.(DCReq)
return req, nil
}
func DecodeClusterID(c context.Context, r *http.Request) (string, error) {
clusterID := mux.Vars(r)["cluster_id"]
if clusterID == "" {
return "", fmt.Errorf("'cluster_id' parameter is required but was not provided")
}
return clusterID, nil
}
// UserIDGetter knows how to get user ID from the request.
type UserIDGetter interface {
GetUserID() string
}
func DecodeSSHKeyID(c context.Context, r *http.Request) (string, error) {
keyID := mux.Vars(r)["key_id"]
if keyID == "" {
return "", fmt.Errorf("'key_id' parameter is required but was not provided")
}
return keyID, nil
}
// DecodeNamespace decodes the namespace out of http.Request's path.
func DecodeNamespace(c context.Context, r *http.Request) (string, error) {
namespace := mux.Vars(r)["namespace"]
if namespace == "" {
return "", fmt.Errorf("'namespace' parameter is required but was not provided")
}
return namespace, nil
}