-
Notifications
You must be signed in to change notification settings - Fork 153
/
vsphere.go
137 lines (116 loc) · 4.67 KB
/
vsphere.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
/*
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 provider
import (
"context"
"crypto/x509"
"fmt"
"net/http"
"github.com/go-kit/kit/endpoint"
apiv1 "k8c.io/kubermatic/v2/pkg/api/v1"
providercommon "k8c.io/kubermatic/v2/pkg/handler/common/provider"
"k8c.io/kubermatic/v2/pkg/handler/v1/common"
"k8c.io/kubermatic/v2/pkg/handler/v2/cluster"
"k8c.io/kubermatic/v2/pkg/provider"
"k8c.io/kubermatic/v2/pkg/util/errors"
)
func VsphereNetworksWithClusterCredentialsEndpoint(projectProvider provider.ProjectProvider,
privilegedProjectProvider provider.PrivilegedProjectProvider, seedsGetter provider.SeedsGetter,
userInfoGetter provider.UserInfoGetter, caBundle *x509.CertPool) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(vSphereNoCredentialsReq)
return providercommon.VsphereNetworksWithClusterCredentialsEndpoint(ctx, userInfoGetter, projectProvider,
privilegedProjectProvider, seedsGetter, req.ProjectID, req.ClusterID, caBundle)
}
}
func VsphereFoldersWithClusterCredentialsEndpoint(projectProvider provider.ProjectProvider,
privilegedProjectProvider provider.PrivilegedProjectProvider, seedsGetter provider.SeedsGetter,
userInfoGetter provider.UserInfoGetter, caBundle *x509.CertPool) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(vSphereNoCredentialsReq)
return providercommon.VsphereFoldersWithClusterCredentialsEndpoint(ctx, userInfoGetter, projectProvider,
privilegedProjectProvider, seedsGetter, req.ProjectID, req.ClusterID, caBundle)
}
}
func VsphereDatastoreEndpoint(seedsGetter provider.SeedsGetter, presetsProvider provider.PresetProvider,
userInfoGetter provider.UserInfoGetter, caBundle *x509.CertPool) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req, ok := request.(vSphereDatastoresReq)
if !ok {
return nil, fmt.Errorf("incorrect type of request, expected = VSphereFoldersReq, got = %T", request)
}
userInfo, err := userInfoGetter(ctx, "")
if err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
username := req.Username
password := req.Password
if len(req.Credential) > 0 {
preset, err := presetsProvider.GetPreset(userInfo, req.Credential)
if err != nil {
return nil, errors.New(http.StatusInternalServerError, fmt.Sprintf("can not get preset %s for user %s", req.Credential, userInfo.Email))
}
if credentials := preset.Spec.VSphere; credentials != nil {
username = credentials.Username
password = credentials.Password
}
}
return providercommon.GetVsphereDatastoreList(userInfo, seedsGetter, username, password, req.DatacenterName, caBundle)
}
}
// VSphereFoldersReq represent a request for vsphere datastores
// swagger:parameters listVSphereDatastores
type vSphereDatastoresReq struct {
// in: header
Username string
// in: header
Password string
// in: header
DatacenterName string
// in: header
// Credential predefined Kubermatic credential name from the presets
Credential string
}
func DecodeVSphereDatastoresReq(_ context.Context, r *http.Request) (interface{}, error) {
var req vSphereDatastoresReq
req.Username = r.Header.Get("Username")
req.Password = r.Header.Get("Password")
req.DatacenterName = r.Header.Get("DatacenterName")
req.Credential = r.Header.Get("Credential")
return req, nil
}
// vSphereNoCredentialsReq represent a request for vsphere networks
// swagger:parameters listVSphereNetworksNoCredentialsV2 listVSphereFoldersNoCredentialsV2
type vSphereNoCredentialsReq struct {
cluster.GetClusterReq
}
// GetSeedCluster returns the SeedCluster object
func (req vSphereNoCredentialsReq) GetSeedCluster() apiv1.SeedCluster {
return apiv1.SeedCluster{
ClusterID: req.ClusterID,
}
}
func DecodeVSphereNoCredentialsReq(c context.Context, r *http.Request) (interface{}, error) {
var req vSphereNoCredentialsReq
clusterID, err := common.DecodeClusterID(c, r)
if err != nil {
return nil, err
}
req.ClusterID = clusterID
pr, err := common.DecodeProjectRequest(c, r)
if err != nil {
return nil, err
}
req.ProjectReq = pr.(common.ProjectReq)
return req, nil
}