-
Notifications
You must be signed in to change notification settings - Fork 527
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: check name existed when creating or updating a resource #1606
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1606 +/- ##
==========================================
- Coverage 71.37% 70.90% -0.48%
==========================================
Files 133 47 -86
Lines 5384 3073 -2311
Branches 592 0 -592
==========================================
- Hits 3843 2179 -1664
+ Misses 1297 651 -646
+ Partials 244 243 -1
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
api/internal/handler/route/route.go
Outdated
return &data.SpecCodeResponse{StatusCode: http.StatusInternalServerError}, err | ||
} | ||
if ret.TotalSize > 0 { | ||
return &data.SpecCodeResponse{StatusCode: http.StatusBadRequest}, errors.New("route name is existed") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo: "route name is existed" => "route name exists".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed.
api/internal/handler/route/route.go
Outdated
@@ -489,12 +503,32 @@ func (h *Handler) Update(c droplet.Context) (interface{}, error) { | |||
} | |||
} | |||
|
|||
ret, err := h.routeStore.Update(c.Context(), &input.Route, true) | |||
// check name existed | |||
ret, err := h.routeStore.List(c.Context(), store.ListInput{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All these logics are similar, can we abstract them to a common interface/method, so that we can reduce the code complexity and redundency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can use reflection to get the name filed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated. thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
api/test/e2enew/route/route_test.go
Outdated
Method: http.MethodPut, | ||
Path: "/apisix/admin/routes/r2", | ||
Body: `{ | ||
"name": "route1", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bad indentation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed.
api/test/e2enew/route/route_test.go
Outdated
}`, | ||
Headers: map[string]string{"Authorization": base.GetToken()}, | ||
ExpectStatus: http.StatusBadRequest, | ||
ExpectBody: `route name is existed`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the grammar is right
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed. @moonming
case "upstream": | ||
objID = obj.(*entity.Upstream).ID | ||
objName = obj.(*entity.Upstream).Name | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a default arm can be specified here:
default:
panic("bad resource")
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed. @tokers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
code style is good to me,but I am not check the logic
Path: "/apisix/admin/routes/r2", | ||
Headers: map[string]string{"Authorization": base.GetToken()}, | ||
ExpectStatus: http.StatusOK, | ||
}), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO, we should add test cases that verify the data is deleted after deleting route data.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed.
api/test/e2enew/route/route_test.go
Outdated
table.Entry("hit route2", base.HttpTestCase{ | ||
Object: base.APISIXExpect(), | ||
Method: http.MethodGet, | ||
Path: "/hello_", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The path of route2 is updated to "/hello". This test case cannot be tested "hit route2".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed. thanks @Jaycean
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Hi @nic-chen , I found that currently, fe called |
I think we could keep it. In this way, the user can know whether the name is available on the first step of creating a route. |
ok |
Please answer these questions before submitting a pull request
Why submit this pull request?
Bugfix
New feature provided
Improve performance
Backport patches
Related issues
#1595
Bugfix
When creating route, server and upstream in the Dashboard, the FE queries whether the same name exists through API, and does not run the addition if it exists. But the backend lacks this limitation.
Add a judgment on whether the name exists in the backend, if it exists, it is not allowed to create or update.