Skip to content
This repository has been archived by the owner on Jan 22, 2024. It is now read-only.

Commit

Permalink
instance update api (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
MI-cool committed Jul 24, 2023
1 parent 43997e3 commit fa965e1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
23 changes: 23 additions & 0 deletions v1/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func (n *NacosV1Server) AddServiceAccess(ws *restful.WebService) {

func (n *NacosV1Server) addInstanceAccess(ws *restful.WebService) {
ws.Route(ws.POST("/instance").To(n.RegisterInstance))
ws.Route(ws.PUT("/instance").To(n.UpdateInstance))
ws.Route(ws.DELETE("/instance").To(n.DeRegisterInstance))
ws.Route(ws.PUT("/instance/beat").To(n.Heartbeat))
ws.Route(ws.GET("/instance/list").To(n.ListInstances))
Expand Down Expand Up @@ -116,6 +117,28 @@ func (n *NacosV1Server) RegisterInstance(req *restful.Request, rsp *restful.Resp
core.WrirteSimpleResponse("ok", http.StatusOK, rsp)
}

func (n *NacosV1Server) UpdateInstance(req *restful.Request, rsp *restful.Response) {
handler := Handler{
Request: req,
Response: rsp,
}

namespace := optional(req, model.ParamNamespaceID, model.DefaultNacosNamespace)
namespace = model.ToPolarisNamespace(namespace)
ins, err := BuildInstance(namespace, req)
if err != nil {
core.WrirteNacosErrorResponse(err, rsp)
return
}

ctx := handler.ParseHeaderContext()
if err := n.handleUpdate(ctx, namespace, ins.ServiceName, ins); err != nil {
core.WrirteNacosErrorResponse(err, rsp)
return
}
core.WrirteSimpleResponse("ok", http.StatusOK, rsp)
}

func (n *NacosV1Server) DeRegisterInstance(req *restful.Request, rsp *restful.Response) {
handler := Handler{
Request: req,
Expand Down
12 changes: 12 additions & 0 deletions v1/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ func (n *NacosV1Server) handleRegister(ctx context.Context, namespace, serviceNa
return nil
}

func (n *NacosV1Server) handleUpdate(ctx context.Context, namespace, serviceName string, ins *model.Instance) error {
specIns := model.PrepareSpecInstance(namespace, serviceName, ins)
resp := n.discoverSvr.UpdateInstance(ctx, specIns)
if apimodel.Code(resp.GetCode().GetValue()) != apimodel.Code_ExecuteSuccess {
return &model.NacosError{
ErrCode: int32(model.ExceptionCode_ServerError),
ErrMsg: resp.GetInfo().GetValue(),
}
}
return nil
}

func (n *NacosV1Server) handleDeregister(ctx context.Context, namespace, service string, ins *model.Instance) error {
specIns := model.PrepareSpecInstance(namespace, service, ins)
resp := n.discoverSvr.DeregisterInstance(ctx, specIns)
Expand Down

0 comments on commit fa965e1

Please sign in to comment.