forked from kubermatic/kubermatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
128 lines (104 loc) · 3.35 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
/*
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"
)
// 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
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"`
}
// GetDC returns the name of the datacenter in the request
func (req DCReq) GetDC() string {
return 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
}