01e5d222b4950e8f7f36ee721def95595cd5da9d
[ealt-edge.git] / mep / mepserver / mp1 / plan_get_one_subscribe.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         "encoding/json"
22         "net/http"
23
24         "github.com/apache/servicecomb-service-center/pkg/log"
25         "github.com/apache/servicecomb-service-center/server/core/backend"
26         "github.com/apache/servicecomb-service-center/server/core/proto"
27         "github.com/apache/servicecomb-service-center/server/plugin/pkg/registry"
28
29         "mepserver/mp1/arch/workspace"
30         "mepserver/mp1/models"
31 )
32
33 type GetOneSubscribe struct {
34         workspace.TaskBase
35         R             *http.Request       `json:"r,in"`
36         HttpErrInf    *proto.Response     `json:"httpErrInf,out"`
37         Ctx           context.Context     `json:"ctx,in"`
38         W             http.ResponseWriter `json:"w,in"`
39         AppInstanceId string              `json:"appInstanceId,in"`
40         SubscribeId   string              `json:"subscribeId,in"`
41         HttpRsp       interface{}         `json:"httpRsp,out"`
42 }
43
44 func (t *GetOneSubscribe) OnRequest(data string) workspace.TaskCode {
45
46         appInstanceId := t.AppInstanceId
47         subscribeId := t.SubscribeId
48
49         opts := []registry.PluginOp{
50                 registry.OpGet(registry.WithStrKey("/cse-sr/etsi/subscribe/" + appInstanceId + "/" + subscribeId)),
51         }
52         resp, err := backend.Registry().TxnWithCmp(context.Background(), opts, nil, nil)
53         if err != nil {
54                 log.Errorf(err, "get subscription from etcd failed")
55                 t.SetFirstErrorCode(OperateDataWithEtcdErr, "get subscription from etch failed")
56                 return workspace.TaskFinish
57         }
58
59         if len(resp.Kvs) == 0 {
60                 log.Errorf(err, "subscription not exist")
61                 t.SetFirstErrorCode(SubscriptionNotFound, "subscription not exist")
62                 return workspace.TaskFinish
63         }
64         sub := &models.SerAvailabilityNotificationSubscription{}
65         jsonErr := json.Unmarshal([]byte(string(resp.Kvs[0].Value)), sub)
66         if jsonErr != nil {
67                 log.Warn("subscribe can not be parsed to SerAvailabilityNotificationSubscription")
68                 return workspace.TaskFinish
69         }
70         t.HttpRsp = sub
71         return workspace.TaskFinish
72 }