-
Notifications
You must be signed in to change notification settings - Fork 6
/
service_binding.go
142 lines (130 loc) · 5.6 KB
/
service_binding.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
/*
* Copyright 2022 Huawei Cloud Computing Technologies Co., Ltd
*
* 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 manager
import (
"fmt"
"net/http"
"github.com/beego/beego/v2/server/web"
"k8s.io/klog/v2"
"github.com/kappital/kappital/pkg/apis"
"github.com/kappital/kappital/pkg/constants"
"github.com/kappital/kappital/pkg/controller/utils"
"github.com/kappital/kappital/pkg/resource"
"github.com/kappital/kappital/pkg/utils/errors"
"github.com/kappital/kappital/pkg/utils/validation"
)
// ServiceBindingController the controller of the service binding which deploy, search, and delete the service binding
// in database or cluster
type ServiceBindingController struct {
web.Controller
resource resource.ServiceBindingResource
}
// CreateServiceBinding deploy the service package into cluster
func (s *ServiceBindingController) CreateServiceBinding() {
serviceBody, err := getAndResolveServiceParam(s.Ctx)
var resourceName string
defer utils.AuditLog(s.Ctx, "CreateServiceBinding", utils.DeployAction, &resourceName, &err)
if err != nil {
utils.ReplyJSON(s.Ctx, http.StatusBadRequest, errors.ErrServiceParam.WrapErrorReasonWith(err.Error()))
return
}
resourceName = fmt.Sprintf("Deploy Service [%s] into Cluster [%s]", serviceBody.Service.Name, serviceBody.ClusterID)
sb, err := s.resource.GetInternalServiceBinding(serviceBody.Service.Spec.Description.Name, serviceBody.ClusterID)
if err != nil {
utils.ReplyJSON(s.Ctx, http.StatusInternalServerError,
errors.ErrServiceInstall.WrapErrorReasonWith(err.Error()))
return
}
if sb != nil {
utils.ReplyJSON(s.Ctx, http.StatusOK, map[string]string{"Name": sb.Name, "ID": sb.ID})
return
}
subRes, err := transCreationToServiceBinding(serviceBody)
if err != nil {
utils.ReplyJSON(s.Ctx, http.StatusBadRequest, errors.ErrServiceParam.WrapErrorReasonWith(err.Error()))
return
}
if err = s.resource.CreateServiceBinding(*subRes); err != nil {
utils.ReplyJSON(s.Ctx, http.StatusInternalServerError,
errors.ErrServiceInstall.WrapErrorReasonWith(err.Error()))
return
}
klog.Infof("service %s serviceBinding create in cluster %s success.", subRes.Name, subRes.ClusterID)
utils.ReplyJSON(s.Ctx, http.StatusOK, map[string]string{"Name": subRes.Name, "ID": subRes.ID})
}
// DeleteServiceBinding destroy the service binding from cluster
func (s *ServiceBindingController) DeleteServiceBinding() {
serviceBinding := s.GetString(constants.ServiceBindingPathParam)
clusterName := s.GetString(constants.ClusterNameQueryParam, apis.DefaultCluster)
var err error
var resourceName string
defer utils.AuditLog(s.Ctx, "DeleteServiceBinding", utils.UninstallAction, &resourceName, &err)
if !utils.ValidString(serviceBinding) || !utils.ValidString(clusterName) {
err = utils.ErrIllegalParameters
utils.ReplyJSON(s.Ctx, http.StatusBadRequest, utils.ErrIllegalParameters)
return
}
resourceName = fmt.Sprintf("Uninstall Service Binding [%s] in Cluster [%s]", serviceBinding, clusterName)
if err = s.resource.DeleteServiceBinding(serviceBinding, clusterName); err != nil {
utils.ReplyJSON(s.Ctx, http.StatusInternalServerError, errors.ErrServiceDelete.WrapErrorReasonWith(err.Error()))
return
}
utils.ReplyJSON(s.Ctx, http.StatusOK, nil)
}
// GetServiceBindings get the service bindings' information from database and cluster
func (s *ServiceBindingController) GetServiceBindings() {
clusterName := s.GetString(constants.ClusterNameQueryParam, apis.DefaultCluster)
var err error
var resourceName string
defer utils.AuditLog(s.Ctx, "GetServiceBindings", utils.QueryAction, &resourceName, &err)
if !utils.ValidString(clusterName) {
err = utils.ErrIllegalParameters
utils.ReplyJSON(s.Ctx, http.StatusBadRequest, utils.ErrIllegalParameters)
return
}
resourceName = fmt.Sprintf("Get Service Binding List from Cluster [%s]", clusterName)
sis, err := s.resource.GetServiceBindings(clusterName)
if err != nil {
utils.ReplyJSON(s.Ctx, http.StatusInternalServerError, err)
return
}
utils.ReplyJSON(s.Ctx, http.StatusOK, sis)
}
// GetServiceBindingDetail get the service binding detail information
func (s *ServiceBindingController) GetServiceBindingDetail() {
serviceBinding := s.GetString(constants.ServiceBindingPathParam)
clusterName := s.GetString(constants.ClusterNameQueryParam, apis.DefaultCluster)
var err error
var resourceName string
defer utils.AuditLog(s.Ctx, "GetServiceBindingDetail", utils.QueryAction, &resourceName, &err)
if !utils.ValidString(serviceBinding) || !utils.ValidString(clusterName) {
err = utils.ErrIllegalParameters
utils.ReplyJSON(s.Ctx, http.StatusBadRequest, utils.ErrIllegalParameters)
return
}
resourceName = fmt.Sprintf("Get Service Binding [%s] from Cluster [%s]", serviceBinding, clusterName)
detail, err := validation.ValidBool(s.Ctx.Input.Query(constants.Detail))
if err != nil {
klog.Warningf("cannot get parameter value of detail, will set it to the false")
}
si, err := s.resource.GetServiceBinding(serviceBinding, clusterName, detail)
if err != nil || si == nil {
err = fmt.Errorf("cannot get the service binding")
utils.ReplyJSON(s.Ctx, http.StatusBadRequest, err)
return
}
utils.ReplyJSON(s.Ctx, http.StatusOK, si)
}