cf3477015d0c091dbb30959913204a7691650946
[ealt-edge.git] / mep / mepserver / mp1 / plan_get_subsribes.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 GetSubscribes 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         HttpRsp       interface{}         `json:"httpRsp,out"`
41 }
42
43 func (t *GetSubscribes) OnRequest(data string) workspace.TaskCode {
44
45         appInstanceId := t.AppInstanceId
46
47         opts := []registry.PluginOp{
48                 registry.OpGet(registry.WithStrKey("/cse-sr/etsi/subscribe/"+appInstanceId), registry.WithPrefix()),
49         }
50         resp, err := backend.Registry().TxnWithCmp(context.Background(), opts, nil, nil)
51         if err != nil {
52                 log.Errorf(err, "get subscription from etcd failed")
53                 t.SetFirstErrorCode(OperateDataWithEtcdErr, "get subscription from etcd failed")
54                 return workspace.TaskFinish
55         }
56
57         var values []string
58         for _, value := range resp.Kvs {
59                 values = append(values, string(value.Value))
60         }
61         if len(values) == 0 {
62                 log.Errorf(err, "get subscription failed, subscription not exist")
63                 t.SetFirstErrorCode(SubscriptionNotFound, "get subscription failed, subscription not exist")
64                 return workspace.TaskFinish
65         }
66         subs := make([]*models.SerAvailabilityNotificationSubscription, 0, len(values))
67         for _, val := range values {
68                 sub := &models.SerAvailabilityNotificationSubscription{}
69                 err := json.Unmarshal([]byte(val), sub)
70                 if err != nil {
71                         log.Warn("subscribe can not be parsed to SerAvailabilityNotificationSubscription")
72                         continue
73                 }
74                 subs = append(subs, sub)
75         }
76         t.HttpRsp = subs
77         return workspace.TaskFinish
78 }