2 * Copyright 2020 Huawei Technologies Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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"
26 "mepserver/mp1/arch/workspace"
27 "mepserver/mp1/models"
28 meputil "mepserver/mp1/util"
31 type UpdateInstance struct {
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"`
40 func (t *UpdateInstance) OnRequest(data string) workspace.TaskCode {
41 if t.ServiceId == "" {
42 t.SetFirstErrorCode(SerErrFailBase, "param is empty")
43 return workspace.TaskFinish
45 mp1Ser, ok := t.RestBody.(*models.ServiceInfo)
47 t.SetFirstErrorCode(SerErrFailBase, "body invalid")
48 return workspace.TaskFinish
51 instance, err := meputil.GetServiceInstance(t.Ctx, t.ServiceId)
53 t.SetFirstErrorCode(SerInstanceNotFound, "find service failed")
54 return workspace.TaskFinish
57 copyInstanceRef := *instance
58 req := proto.RegisterInstanceRequest{
59 Instance: ©InstanceRef,
61 mp1Ser.ToRegisterInstance(&req)
63 domainProject := util.ParseDomainProject(t.Ctx)
64 centerErr := svcutil.UpdateInstance(t.Ctx, domainProject, ©InstanceRef)
66 t.SetFirstErrorCode(SerErrServiceUpdFailed, "update service failed")
67 return workspace.TaskFinish
70 err = meputil.Heartbeat(t.Ctx, mp1Ser.SerInstanceId)
72 t.SetFirstErrorCode(SerErrServiceUpdFailed, "heartbeat failed")
73 return workspace.TaskFinish
75 mp1Ser.SerInstanceId = instance.ServiceId + instance.InstanceId
77 t.HttpErrInf = proto.CreateResponse(proto.Response_SUCCESS, "Update service instance success.")
78 return workspace.TaskFinish