-
Notifications
You must be signed in to change notification settings - Fork 332
/
Copy pathservice.go
210 lines (180 loc) · 6.28 KB
/
service.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright 2019 The OpenSDS Authors.
//
// 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 s3
import (
"context"
"io"
"io/ioutil"
"math"
"math/rand"
"net/http"
"os"
"github.com/micro/go-micro/v2/client"
backend "github.com/opensds/multi-cloud/backend/proto"
s3error "github.com/opensds/multi-cloud/s3/error"
s3 "github.com/opensds/multi-cloud/s3/proto"
"github.com/emicklei/go-restful"
log "github.com/sirupsen/logrus"
"github.com/opensds/multi-cloud/api/pkg/common"
)
const (
MICRO_ENVIRONMENT = "MICRO_ENVIRONMENT"
K8S = "k8s"
s3Service_Docker = "s3"
backendService_Docker = "backend"
s3Service_K8S = "soda.multicloud.v1.s3"
backendService_K8S = "soda.multicloud.v1.backend"
)
type APIService struct {
s3Client s3.S3Service
backendClient backend.BackendService
}
func NewAPIService(c client.Client) *APIService {
s3Service := s3Service_Docker
backendService := backendService_Docker
if os.Getenv(MICRO_ENVIRONMENT) == K8S {
backendService = backendService_K8S
s3Service = s3Service_K8S
}
return &APIService{
s3Client: s3.NewS3Service(s3Service, c),
backendClient: backend.NewBackendService(backendService, c),
}
}
func IsQuery(request *restful.Request, name string) bool {
params := request.Request.URL.Query()
if params == nil {
return false
}
if _, ok := params[name]; ok {
return true
}
return false
}
func HasHeader(request *restful.Request, name string) bool {
param := request.HeaderParameter(name)
if param == "" {
return false
}
return true
}
func ReadBody(r *restful.Request) []byte {
var reader io.Reader = r.Request.Body
b, e := ioutil.ReadAll(reader)
if e != nil {
return nil
}
return b
}
func (s *APIService) getBucketMeta(ctx context.Context, bucketName string) (*s3.Bucket, error) {
rsp, err := s.s3Client.GetBucket(ctx, &s3.Bucket{Name: bucketName})
// according to gRPC framework work mechanism, if gRPC return error, then no response package can be received by
// gRPC client, so in our codes, gRPC server will return nil and set error code to reponse package while business
// error happens, and if gRPC client received error, that means some exception happened for gRPC itself.
if err == nil {
if rsp.GetErrorCode() != int32(s3error.ErrNoErr) {
err = s3error.S3ErrorCode(rsp.GetErrorCode())
}
}
if err != nil {
log.Infof("get bucket meta data[bucket=%s] failed, err=%v\n", bucketName, err)
return nil, err
}
return rsp.BucketMeta, nil
}
// if isHeadReq is true, will return expiration time and the ruleId which cause the expiration, that is need for HeadObject
func (s *APIService) getObjectMeta(ctx context.Context, bucketName, objectName, versiongId string,
isHeadReq bool) (*s3.Object, int64, string, error) {
rsp, err := s.s3Client.GetObjectMeta(ctx, &s3.GetObjectMetaRequest{
BucketName: bucketName, ObjectKey: objectName, VersionId: versiongId, IsHeadReq: isHeadReq})
// according to gRPC framework work mechanism, if gRPC return error, then no response package can be received by
// gRPC client, so in our codes, gRPC server will return nil and set error code to reponse package while business
// error happens, and if gRPC client received error, that means some exception happened for gRPC itself.
if err == nil {
if rsp.GetErrorCode() != int32(s3error.ErrNoErr) {
err = s3error.S3ErrorCode(rsp.GetErrorCode())
}
}
if err != nil {
log.Infof("get object meta data[bucket=%s,key=%s] failed, err=%v\n", bucketName, objectName, err)
return nil, 0, "", err
}
return rsp.Object, rsp.ExpireTime, rsp.RuleId, nil
}
func (s *APIService) isBackendExist(ctx context.Context, backendName string) bool {
flag := false
backendRep, backendErr := s.backendClient.ListBackend(ctx, &backend.ListBackendRequest{
Offset: 0,
Limit: math.MaxInt32,
Filter: map[string]string{"name": backendName}})
log.Infof("backendErr is %v:", backendErr)
if backendErr != nil {
log.Infof("Get backend[name=%s] failed.", backendName)
} else {
if len(backendRep.Backends) > 0 {
log.Infof("backend[name=%s] exist.", backendName)
flag = true
}
}
return flag
}
func HandleS3Error(response *restful.Response, request *restful.Request, err error, errCode int32) error {
if err != nil {
WriteErrorResponse(response, request, err)
return err
}
if errCode != int32(s3error.ErrNoErr) {
err := s3error.S3ErrorCode(errCode)
WriteErrorResponse(response, request, err)
return err
}
return nil
}
func (s *APIService) GetBackendIdFromSsp(ctx context.Context, sspName string) string {
log.Info("Request for GetBackendIdFromTier received", sspName)
var response *restful.Response
var backendId string
res, err := s.backendClient.ListSsps(common.GetAdminContext(), &backend.ListSspRequest{
Limit: common.MaxPaginationLimit,
Offset: common.DefaultPaginationOffset,
Filter: map[string]string{"name": sspName},
})
if err != nil {
log.Error("list ssp failed during getting backends from ssp")
response.WriteError(http.StatusInternalServerError, err)
return ""
}
backendId = res.Ssps[0].Backends[rand.Intn(len(res.Ssps[0].Backends))]
return backendId
}
// this method is basically for getting the backends name from ssp
func (s *APIService) getBackendFromSsp(ctx context.Context, sspName string) string {
log.Info("The received ssp name for getting backend name:", sspName)
var backendId, backendName string
var response *restful.Response
backendId = s.GetBackendIdFromSsp(ctx, sspName)
adminCtx := common.GetAdminContext()
if backendId != "" {
backendRep, err := s.backendClient.GetBackend(adminCtx, &backend.GetBackendRequest{Id: backendId})
if err != nil {
log.Error("the selected backends from ssp doesn't exists.")
response.WriteError(http.StatusInternalServerError, err)
return ""
}
if backendRep != nil {
backendName = backendRep.Backend.Name
}
}
return backendName
}