Skip to content

Commit

Permalink
[fix] modify some error reponse in some error situation (#1479)
Browse files Browse the repository at this point in the history
  • Loading branch information
tornado-ssy committed Jun 20, 2024
1 parent bd2ee25 commit bec7167
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 43 deletions.
2 changes: 1 addition & 1 deletion datasource/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const (
ResourceAccount = "account"
ResourceRole = "role"
ResourceService = "service"
ResourceEnvironment = "env"
ResourceEnvironment = "environment"
ResourceKV = "kv"
ResourceInstance = "instance"
ResourceHeartbeat = "heartbeat"
Expand Down
36 changes: 18 additions & 18 deletions datasource/etcd/ms.go
Original file line number Diff line number Diff line change
Expand Up @@ -1775,7 +1775,7 @@ func (ds *MetadataManager) RegisterEnvironment(ctx context.Context, request *ev.
log.Warn(fmt.Sprintf("create environment[%s] failed, environment already exists, operator: %s",
envFlag, remoteIP))
return nil, pb.NewError(pb.ErrEnvironmentAlreadyExists,
"ServiceID conflict or found the same service with different id.")
"EnvID conflict.")
}
}

Expand Down Expand Up @@ -1861,58 +1861,58 @@ func (ds *MetadataManager) UpdateEnvironment(ctx context.Context, request *ev.Up
}

func (ds *MetadataManager) UnregisterEnvironment(ctx context.Context, request *ev.DeleteEnvironmentRequest) (err error) {
environmentId := request.EnvironmentId
environmentID := request.EnvironmentId
remoteIP := util.GetIPFromContext(ctx)
domainProject := util.ParseDomainProject(ctx)

environment, err := eutil.GetEnvironment(ctx, domainProject, environmentId)
environment, err := eutil.GetEnvironment(ctx, domainProject, environmentID)
if err != nil {
if errors.Is(err, datasource.ErrNoData) {
log.Debug(fmt.Sprintf("environment does not exist, del environmentId[%s] failed, operator: %s",
environmentId, remoteIP))
log.Debug(fmt.Sprintf("environment does not exist, del environmentID[%s] failed, operator: %s",
environmentID, remoteIP))
return pb.NewError(pb.ErrEnvironmentNotExists, "environment does not exist.")
}
log.Error(fmt.Sprintf("del environment[%s] failed, get environment file failed, operator: %s",
environmentId, remoteIP), err)
environmentID, remoteIP), err)
return pb.NewError(pb.ErrInternal, err.Error())
}

serviceEnvKey := path.GenerateServiceEnvIndexKey(domainProject, environment.ID)
if serviceUtil.ServiceEnvExist(ctx, serviceEnvKey) {
log.Error(fmt.Sprintf("del environment[%s] failed, get environment file failed, operator: %s",
environmentId, remoteIP), errors.New("this env has services"))
return pb.NewError(pb.ErrInternal, "this env has services")
environmentID, remoteIP), errors.New("this env has services"))
return pb.NewError(pb.ErrUnregistryedEnv, "this env has services")
}
environmentIdKey := path.GenerateEnvironmentKey(domainProject, environmentId)
environmentIDKey := path.GenerateEnvironmentKey(domainProject, environmentID)
envKey := &ev.EnvironmentKey{
Tenant: domainProject,
Name: environment.Name,
}
opts := []etcdadpt.OpOptions{
etcdadpt.OpDel(etcdadpt.WithStrKey(util.StringJoin([]string{path.GenerateEnvironmentIndexKey(envKey), environmentId}, "/"))),
etcdadpt.OpDel(etcdadpt.WithStrKey(environmentIdKey)),
etcdadpt.OpDel(etcdadpt.WithStrKey(util.StringJoin([]string{path.GenerateEnvironmentIndexKey(envKey), environmentID}, "/"))),
etcdadpt.OpDel(etcdadpt.WithStrKey(environmentIDKey)),
}
syncOpts, err := esync.GenDeleteOpts(ctx, datasource.ResourceEnvironment, environmentId,
&ev.DeleteEnvironmentRequest{EnvironmentId: environmentId})
syncOpts, err := esync.GenDeleteOpts(ctx, datasource.ResourceEnvironment, environmentID,
&ev.DeleteEnvironmentRequest{EnvironmentId: environmentID})
if err != nil {
log.Error("fail to sync opt", err)
return pb.NewError(pb.ErrInternal, err.Error())
}
opts = append(opts, syncOpts...)

resp, err := etcdadpt.TxnWithCmp(ctx, opts, etcdadpt.If(etcdadpt.NotEqualVer(environmentIdKey, 0)), nil)
resp, err := etcdadpt.TxnWithCmp(ctx, opts, etcdadpt.If(etcdadpt.NotEqualVer(environmentIDKey, 0)), nil)
if err != nil {
log.Error(fmt.Sprintf("del environment[%s] failed, operator: %s", environmentId, remoteIP), err)
log.Error(fmt.Sprintf("del environment[%s] failed, operator: %s", environmentID, remoteIP), err)
return pb.NewError(pb.ErrUnavailableBackend, err.Error())
}
if !resp.Succeeded {
log.Error(fmt.Sprintf("del environment[%s] failed, environment does not exist, operator: %s",
environmentId, remoteIP), err)
return pb.NewError(pb.ErrEnvironmentNotExists, "environmentId does not exist.")
environmentID, remoteIP), err)
return pb.NewError(pb.ErrEnvironmentNotExists, "environmentID does not exist.")
}

quotasvc.RemandEnvironment(ctx)

log.Info(fmt.Sprintf("del environment[%s] successfully, operator: %s", environmentId, remoteIP))
log.Info(fmt.Sprintf("del environment[%s] successfully, operator: %s", environmentID, remoteIP))
return nil
}
8 changes: 4 additions & 4 deletions datasource/etcd/path/key_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,10 +387,10 @@ func GenerateMetricsKey(name, utc, domain string) string {
}, SPLIT)
}

func GenerateEnvironmentKey(domainProject string, envId string) string {
func GenerateEnvironmentKey(domainProject string, envID string) string {
return util.StringJoin([]string{
GetEnvironmentRootKey(domainProject),
envId,
envID,
}, SPLIT)
}

Expand Down Expand Up @@ -418,9 +418,9 @@ func GetEnvironmentIndexRootKey(domainProject string) string {
}, SPLIT)
}

func GenerateServiceEnvIndexKey(domainProject string, envId string) string {
func GenerateServiceEnvIndexKey(domainProject string, envID string) string {
return util.StringJoin([]string{
GetServiceIndexRootKey(domainProject),
envId,
envID,
}, SPLIT)
}
10 changes: 5 additions & 5 deletions datasource/mongo/ms.go
Original file line number Diff line number Diff line change
Expand Up @@ -1683,23 +1683,23 @@ func (ds *MetadataManager) UpdateManyInstanceStatus(_ context.Context, _ *dataso
return nil
}

func (ds *MetadataManager) ListEnvironments(ctx context.Context) (*ev.GetEnvironmentsResponse, error) {
func (ds *MetadataManager) ListEnvironments(_ context.Context) (*ev.GetEnvironmentsResponse, error) {
return nil, nil
}

func (ds *MetadataManager) RegisterEnvironment(ctx context.Context, request *ev.CreateEnvironmentRequest) (*ev.CreateEnvironmentResponse, error) {
func (ds *MetadataManager) RegisterEnvironment(_ context.Context, _ *ev.CreateEnvironmentRequest) (*ev.CreateEnvironmentResponse, error) {
return nil, nil
}

func (ds *MetadataManager) GetEnvironment(ctx context.Context, request *ev.GetEnvironmentRequest) (
func (ds *MetadataManager) GetEnvironment(_ context.Context, _ *ev.GetEnvironmentRequest) (
*ev.Environment, error) {
return nil, nil
}

func (ds *MetadataManager) UpdateEnvironment(ctx context.Context, request *ev.UpdateEnvironmentRequest) (err error) {
func (ds *MetadataManager) UpdateEnvironment(_ context.Context, _ *ev.UpdateEnvironmentRequest) (err error) {
return nil
}

func (ds *MetadataManager) UnregisterEnvironment(ctx context.Context, request *ev.DeleteEnvironmentRequest) (err error) {
func (ds *MetadataManager) UnregisterEnvironment(_ context.Context, _ *ev.DeleteEnvironmentRequest) (err error) {
return nil
}
2 changes: 1 addition & 1 deletion datasource/mongo/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,6 @@ func (ds *MetadataManager) getNotGlobalServiceFilter(ctx context.Context) (bson.
return util.NewFilter(util.NotIn(serviceIDs)), nil
}

func (ds *MetadataManager) CountEnvironment(ctx context.Context, request *ev.GetEnvironmentCountRequest) (*ev.GetEnvironmentCountResponse, error) {
func (ds *MetadataManager) CountEnvironment(_ context.Context, _ *ev.GetEnvironmentCountRequest) (*ev.GetEnvironmentCountResponse, error) {
return nil, nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ require (
github.com/cloudflare/gokey v0.1.2
github.com/deckarep/golang-set v1.8.0
github.com/elithrar/simple-scrypt v1.3.0
github.com/go-chassis/cari v0.9.1-0.20240531100749-8955107d022d
github.com/go-chassis/cari v0.9.1-0.20240620034337-b6769eaccc12
github.com/go-chassis/etcdadpt v0.5.3-0.20240328092602-984e34b756fe
github.com/go-chassis/foundation v0.4.0
github.com/go-chassis/go-archaius v1.5.6
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ github.com/go-chassis/cari v0.4.0/go.mod h1:av/19fqwEP4eOC8unL/z67AAbFDwXUCko6SK
github.com/go-chassis/cari v0.5.0/go.mod h1:av/19fqwEP4eOC8unL/z67AAbFDwXUCko6SKa4Avrd8=
github.com/go-chassis/cari v0.5.1-0.20210823023004-74041d1363c4/go.mod h1:av/19fqwEP4eOC8unL/z67AAbFDwXUCko6SKa4Avrd8=
github.com/go-chassis/cari v0.6.0/go.mod h1:mSDRCOQXGmlD69A6NG0hsv0UP1xbVPtL6HCGI6X1tqs=
github.com/go-chassis/cari v0.9.1-0.20240531100749-8955107d022d h1:nQnEl2jV0MEzN+/+019tiwKKliRGiXLGvau4zmTIyIc=
github.com/go-chassis/cari v0.9.1-0.20240531100749-8955107d022d/go.mod h1:ibqLyh+Q+1n9PlldW3glD9G+2s/yeSyVMCCkQWKRwuE=
github.com/go-chassis/cari v0.9.1-0.20240620034337-b6769eaccc12 h1:aIES1QkxyVrCUvR6UD/U8qN9cXHLf3scp2I4+8NbQR4=
github.com/go-chassis/cari v0.9.1-0.20240620034337-b6769eaccc12/go.mod h1:ibqLyh+Q+1n9PlldW3glD9G+2s/yeSyVMCCkQWKRwuE=
github.com/go-chassis/etcdadpt v0.5.3-0.20240328092602-984e34b756fe h1:peLHEt3wzab6nKVcmcu0qkj1+ZXK6D1ymtiyyMBv/XA=
github.com/go-chassis/etcdadpt v0.5.3-0.20240328092602-984e34b756fe/go.mod h1:HV8OZ1Npu+lttD+pJA5nUxWZR3/SBFetTh7w8nYFkUA=
github.com/go-chassis/foundation v0.2.2-0.20201210043510-9f6d3de40234/go.mod h1:2PjwqpVwYEVaAldl5A58a08viH8p27pNeYaiE3ZxOBA=
Expand Down
10 changes: 5 additions & 5 deletions server/resource/disco/environment_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (s *EnvironmentResource) GetEnvironment(w http.ResponseWriter, r *http.Requ

func (s *EnvironmentResource) UpdateEnvironment(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
environmentId := query.Get(":environmentId")
environmentID := query.Get(":environmentId")
message, err := io.ReadAll(r.Body)
if err != nil {
log.Error("read body failed", err)
Expand All @@ -91,7 +91,7 @@ func (s *EnvironmentResource) UpdateEnvironment(w http.ResponseWriter, r *http.R
rest.WriteError(w, pb.ErrInvalidParams, err.Error())
return
}
request.Environment.ID = environmentId
request.Environment.ID = environmentID
err = discosvc.UpdateEnvironment(r.Context(), &request)
if err != nil {
log.Error("update environment failed", err)
Expand All @@ -103,14 +103,14 @@ func (s *EnvironmentResource) UpdateEnvironment(w http.ResponseWriter, r *http.R

func (s *EnvironmentResource) UnRegistryEnvironment(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
environmentId := query.Get(":environmentId")
environmentID := query.Get(":environmentId")

request := &ev.DeleteEnvironmentRequest{
EnvironmentId: environmentId,
EnvironmentId: environmentID,
}
err := discosvc.UnRegistryEnvironment(r.Context(), request)
if err != nil {
log.Error(fmt.Sprintf("delete environment[%s] failed", environmentId), err)
log.Error(fmt.Sprintf("delete environment[%s] failed", environmentID), err)
rest.WriteServiceError(w, err)
return
}
Expand Down
12 changes: 6 additions & 6 deletions syncer/service/replicator/resource/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type environment struct {
updateInput *ev.UpdateEnvironmentRequest
deleteInput *ev.DeleteEnvironmentRequest

envId string
envID string

cur *ev.Environment

Expand All @@ -62,17 +62,17 @@ type environmentManager interface {
func (e *environment) loadInput() error {
e.createInput = new(ev.CreateEnvironmentRequest)
cre := newInputParam(e.createInput, func() {
e.envId = e.createInput.Environment.ID
e.envID = e.createInput.Environment.ID
})

e.updateInput = new(ev.UpdateEnvironmentRequest)
upd := newInputParam(e.updateInput, func() {
e.envId = e.updateInput.Environment.ID
e.envID = e.updateInput.Environment.ID
})

e.deleteInput = new(ev.DeleteEnvironmentRequest)
del := newInputParam(e.deleteInput, func() {
e.envId = e.deleteInput.EnvironmentId
e.envID = e.deleteInput.EnvironmentId
})

return newInputLoader(
Expand All @@ -90,7 +90,7 @@ func (e *environment) LoadCurrentResource(ctx context.Context) *Result {
}

cur, err := e.manager.GetEnvironment(ctx, &ev.GetEnvironmentRequest{
EnvironmentId: e.envId,
EnvironmentId: e.envID,
})
if err != nil {
if errsvc.IsErrEqualCode(err, pb.ErrServiceNotExists) {
Expand All @@ -110,7 +110,7 @@ func (e *environment) NeedOperate(ctx context.Context) *Result {
updateTime: func() (int64, error) {
return formatUpdateTimeSecond(e.cur.ModTimestamp)
},
resourceID: e.envId,
resourceID: e.envID,
}
c.tombstoneLoader = c
return c.needOperate(ctx)
Expand Down

0 comments on commit bec7167

Please sign in to comment.