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