Merge "mecm-mepm uninstall playbook added"
[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
51         resp, err := backend.Registry().TxnWithCmp(context.Background(), opts, nil, nil)
52         if err != nil {
53                 log.Errorf(err, "get subscription from etcd failed")
54                 t.SetFirstErrorCode(OperateDataWithEtcdErr, "get subscription from etcd failed")
55                 return workspace.TaskFinish
56         }
57
58         var values []string
59         for _, value := range resp.Kvs {
60                 values = append(values, string(value.Value))
61         }
62         if len(values) == 0 {
63                 log.Errorf(err, "get subscription failed, subscription not exist")
64                 t.SetFirstErrorCode(SubscriptionNotFound, "get subscription failed, subscription not exist")
65                 return workspace.TaskFinish
66         }
67
68         subs := make([]*models.SerAvailabilityNotificationSubscription, 0, len(values))
69         for _, val := range values {
70                 sub := &models.SerAvailabilityNotificationSubscription{}
71                 err := json.Unmarshal([]byte(val), sub)
72                 if err != nil {
73                         log.Warn("subscribe can not be parsed to SerAvailabilityNotificationSubscription")
74                         continue
75                 }
76                 subs = append(subs, sub)
77         }
78
79         t.HttpRsp = subs
80         return workspace.TaskFinish
81 }