-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathresource_group.go
More file actions
73 lines (64 loc) · 2.98 KB
/
resource_group.go
File metadata and controls
73 lines (64 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright The Karpor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package resourcegroup
import (
"net/http"
"github.com/KusionStack/karpor/pkg/infra/search/storage"
"github.com/KusionStack/karpor/pkg/core/handler"
"github.com/KusionStack/karpor/pkg/core/manager/resourcegroup"
"github.com/KusionStack/karpor/pkg/util/ctxutil"
"github.com/go-chi/chi/v5"
"github.com/pkg/errors"
)
// List returns an HTTP handler function that lists all ResourceGroup by
// specified rule. It utilizes a ResourceGroupManager to execute the logic.
//
// @Summary List lists all ResourceGroups by rule name.
// @Description This endpoint lists all ResourceGroups.
// @Tags resourcegroup
// @Produce json
// @Param resourceGroupRuleName path string true "The name of the resource group rule"
// @Success 200 {array} unstructured.Unstructured "List of resourceGroup objects"
// @Failure 400 {string} string "Bad Request"
// @Failure 401 {string} string "Unauthorized"
// @Failure 404 {string} string "Not Found"
// @Failure 405 {string} string "Method Not Allowed"
// @Failure 429 {string} string "Too Many Requests"
// @Failure 500 {string} string "Internal Server Error"
// @Router /rest-api/v1/resource-groups/{resourceGroupRuleName} [get]
func List(resourceGroupMgr *resourcegroup.ResourceGroupManager) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Extract the context and logger from the request.
ctx := r.Context()
logger := ctxutil.GetLogger(ctx)
name := chi.URLParam(r, "resourceGroupRuleName")
if len(name) == 0 {
handler.FailureRender(ctx, w, r, errors.New("resource group rule name cannot be empty"))
return
}
logger.Info("Listing resourceGroups by resourceGroupRule ...", "resourceGroupRule", name)
// Use the ResourceGroupManager to list resource groups by specified rule.
rgs, err := resourceGroupMgr.ListResourceGroupsBy(ctx, name)
if err != nil {
if errors.Is(err, storage.ErrResourceGroupNotFound) {
handler.NotFoundRender(ctx, w, r, err)
} else {
handler.FailureRender(ctx, w, r, err)
}
return
}
// Render the list of resource groups.
handler.SuccessRender(ctx, w, r, rgs)
}
}