Skip to content
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

Merged
merged 13 commits into from
Mar 18, 2021

Conversation

nic-chen
Copy link
Member

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

  • Description

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.

  • How to fix?

Add a judgment on whether the name exists in the backend, if it exists, it is not allowed to create or update.

@nic-chen nic-chen changed the title fix: check name existed when creating a resource fix: check name existed when creating or updating a resource Mar 17, 2021
@codecov-io
Copy link

codecov-io commented Mar 17, 2021

Codecov Report

Merging #1606 (63a798f) into master (4b8774f) will decrease coverage by 0.47%.
The diff coverage is 96.49%.

Impacted file tree graph

@@            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     
Flag Coverage Δ
backend-e2e-test 61.56% <92.98%> (+0.50%) ⬆️
backend-e2e-test-ginkgo 47.08% <92.98%> (+0.45%) ⬆️
backend-unit-test 51.26% <43.85%> (-0.36%) ⬇️
frontend-e2e-test ?

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
api/internal/handler/handler.go 77.77% <92.59%> (+14.81%) ⬆️
api/internal/handler/route/route.go 78.77% <100.00%> (+0.53%) ⬆️
api/internal/handler/service/service.go 92.00% <100.00%> (+0.51%) ⬆️
api/internal/handler/upstream/upstream.go 89.38% <100.00%> (+0.59%) ⬆️
...eb/src/pages/PluginTemplate/components/Preview.tsx
web/src/components/Plugin/data.tsx
web/src/access.ts
web/src/components/PluginOrchestration/service.ts
web/src/pages/User/Login.tsx
web/src/pages/Route/transform.ts
... and 81 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4b8774f...63a798f. Read the comment docs.

@nic-chen nic-chen requested review from starsz, imjoey, Jaycean, gxthrj and tokers and removed request for starsz, imjoey and Jaycean March 17, 2021 09:03
api/internal/handler/route/route.go Outdated Show resolved Hide resolved
return &data.SpecCodeResponse{StatusCode: http.StatusInternalServerError}, err
}
if ret.TotalSize > 0 {
return &data.SpecCodeResponse{StatusCode: http.StatusBadRequest}, errors.New("route name is existed")
Copy link
Contributor

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".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

@@ -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{
Copy link
Contributor

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea.

Copy link
Contributor

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated. thanks.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tokers @starsz please review again when you have time.

Method: http.MethodPut,
Path: "/apisix/admin/routes/r2",
Body: `{
"name": "route1",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bad indentation

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

}`,
Headers: map[string]string{"Authorization": base.GetToken()},
ExpectStatus: http.StatusBadRequest,
ExpectBody: `route name is existed`,
Copy link
Member

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

Copy link
Member Author

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
}
Copy link
Contributor

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")
			}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed. @tokers

Copy link
Member

@moonming moonming left a 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,
}),
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

table.Entry("hit route2", base.HttpTestCase{
Object: base.APISIXExpect(),
Method: http.MethodGet,
Path: "/hello_",
Copy link
Member

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".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed. thanks @Jaycean

Copy link
Member

@Jaycean Jaycean left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@liuxiran
Copy link
Contributor

Hi @nic-chen , I found that currently, fe called /apisix/admin/notexist/routes to check unique name of route, we can remove calling this api after this pr merged, right?

@nic-chen
Copy link
Member Author

Hi @nic-chen , I found that currently, fe called /apisix/admin/notexist/routes to check unique name of route, we can remove calling this api after this pr merged, right?

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.

@liuxiran
Copy link
Contributor

Hi @nic-chen , I found that currently, fe called /apisix/admin/notexist/routes to check unique name of route, we can remove calling this api after this pr merged, right?

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.

Hi @nic-chen , I found that currently, fe called /apisix/admin/notexist/routes to check unique name of route, we can remove calling this api after this pr merged, right?

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants