d2a7309f06b5af2aedc5d7cfa4fb842f6d8f748b
[ealt-edge.git] / mep / mepserver / mp1 / plan_get_one_svc.go
1 /*
2  * Copyright 2020 Huawei Technologies Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package mp1
18
19 import (
20         "context"
21         "net/http"
22
23         "github.com/apache/servicecomb-service-center/pkg/util"
24         "github.com/apache/servicecomb-service-center/server/core"
25         "github.com/apache/servicecomb-service-center/server/core/proto"
26
27         "mepserver/mp1/arch/workspace"
28         "mepserver/mp1/models"
29         meputil "mepserver/mp1/util"
30 )
31
32 type GetOneDecode struct {
33         workspace.TaskBase
34         R           *http.Request   `json:"r,in"`
35         Ctx         context.Context `json:"ctx,out"`
36         CoreRequest interface{}     `json:"coreRequest,out"`
37 }
38
39 func (t *GetOneDecode) OnRequest(data string) workspace.TaskCode {
40         t.Ctx, t.CoreRequest = t.getFindParam(t.R)
41         return workspace.TaskFinish
42
43 }
44
45 func (t *GetOneDecode) getFindParam(r *http.Request) (context.Context, *proto.GetOneInstanceRequest) {
46         query, ids := meputil.GetHTTPTags(r)
47         mp1SrvId := query.Get(":serviceId")
48         serviceId := mp1SrvId[:len(mp1SrvId)/2]
49         instanceId := mp1SrvId[len(mp1SrvId)/2:]
50         req := &proto.GetOneInstanceRequest{
51                 ConsumerServiceId:  r.Header.Get("X-ConsumerId"),
52                 ProviderServiceId:  serviceId,
53                 ProviderInstanceId: instanceId,
54                 Tags:               ids,
55         }
56
57         ctx := util.SetTargetDomainProject(r.Context(), r.Header.Get("X-Domain-Name"), query.Get(":project"))
58         return ctx, req
59 }
60
61 type GetOneInstance struct {
62         workspace.TaskBase
63         HttpErrInf  *proto.Response `json:"httpErrInf,out"`
64         Ctx         context.Context `json:"ctx,in"`
65         CoreRequest interface{}     `json:"coreRequest,in"`
66         HttpRsp     interface{}     `json:"httpRsp,out"`
67 }
68
69 func (t *GetOneInstance) OnRequest(data string) workspace.TaskCode {
70         resp, _ := core.InstanceAPI.GetOneInstance(t.Ctx, t.CoreRequest.(*proto.GetOneInstanceRequest))
71         t.HttpErrInf = resp.Response
72         resp.Response = nil
73         mp1Rsp := &models.ServiceInfo{}
74         if resp.Instance != nil {
75                 mp1Rsp.FromServiceInstance(resp.Instance)
76         } else {
77                 t.SetFirstErrorCode(SerInstanceNotFound, "service instance id not found")
78                 return workspace.TaskFinish
79         }
80         t.HttpRsp = mp1Rsp
81
82         return workspace.TaskFinish
83 }