Merge "mecm-mepm uninstall playbook added"
[ealt-edge.git] / mep / mepserver / mp1 / plan_update_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
22         "github.com/apache/servicecomb-service-center/pkg/util"
23         "github.com/apache/servicecomb-service-center/server/core/proto"
24         svcutil "github.com/apache/servicecomb-service-center/server/service/util"
25
26         "mepserver/mp1/arch/workspace"
27         "mepserver/mp1/models"
28         meputil "mepserver/mp1/util"
29 )
30
31 type UpdateInstance struct {
32         workspace.TaskBase
33         HttpErrInf *proto.Response `json:"httpErrInf,out"`
34         Ctx        context.Context `json:"ctx,in"`
35         ServiceId  string          `json:"serviceId,in"`
36         RestBody   interface{}     `json:"restBody,in"`
37         HttpRsp    interface{}     `json:"httpRsp,out"`
38 }
39
40 func (t *UpdateInstance) OnRequest(data string) workspace.TaskCode {
41         if t.ServiceId == "" {
42                 t.SetFirstErrorCode(SerErrFailBase, "param is empty")
43                 return workspace.TaskFinish
44         }
45         mp1Ser, ok := t.RestBody.(*models.ServiceInfo)
46         if !ok {
47                 t.SetFirstErrorCode(SerErrFailBase, "body invalid")
48                 return workspace.TaskFinish
49         }
50
51         instance, err := meputil.GetServiceInstance(t.Ctx, t.ServiceId)
52         if err != nil {
53                 t.SetFirstErrorCode(SerInstanceNotFound, "find service failed")
54                 return workspace.TaskFinish
55         }
56
57         copyInstanceRef := *instance
58         req := proto.RegisterInstanceRequest{
59                 Instance: &copyInstanceRef,
60         }
61         mp1Ser.ToRegisterInstance(&req)
62
63         domainProject := util.ParseDomainProject(t.Ctx)
64         centerErr := svcutil.UpdateInstance(t.Ctx, domainProject, &copyInstanceRef)
65         if centerErr != nil {
66                 t.SetFirstErrorCode(SerErrServiceUpdFailed, "update service failed")
67                 return workspace.TaskFinish
68         }
69
70         err = meputil.Heartbeat(t.Ctx, mp1Ser.SerInstanceId)
71         if err != nil {
72                 t.SetFirstErrorCode(SerErrServiceUpdFailed, "heartbeat failed")
73                 return workspace.TaskFinish
74         }
75         mp1Ser.SerInstanceId = instance.ServiceId + instance.InstanceId
76         t.HttpRsp = mp1Ser
77         t.HttpErrInf = proto.CreateResponse(proto.Response_SUCCESS, "Update service instance success.")
78         return workspace.TaskFinish
79 }