Skip to content

Commit

Permalink
Merge pull request meshery#9729 from Yashsharma1911/yash/addFilterInC…
Browse files Browse the repository at this point in the history
…onnectionAPIwq

[Server] Added filters in connections API
  • Loading branch information
Yashsharma1911 authored Dec 27, 2023
2 parents 7b85541 + a555440 commit c778251
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 5 deletions.
41 changes: 39 additions & 2 deletions server/handlers/connections_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@ func (h *Handler) SaveConnection(w http.ResponseWriter, req *http.Request, _ *mo
// ```?page={page-number}``` Default page number is 0
//
// ```?pagesize={pagesize}``` Default pagesize is 10
//
// ```?filter={filter}``` Filter connections with type or sub_type, eg /api/integrations/connections?filter=type%20platform or /api/integrations/connections?filter=sub_type%20management
//
// ```?status={status}``` Status takes array as param to filter connections based on status, eg /api/integrations/connections?status=["connected", "deleted"]
//
// ```?kind={kind}``` Kind takes array as param to filter connections based on kind, eg /api/integrations/connections?kind=["meshery", "kubernetes"]
// responses:
// 200: mesheryConnectionsResponseWrapper
func (h *Handler) GetConnections(w http.ResponseWriter, req *http.Request, prefObj *models.Preference, user *models.User, provider models.Provider) {
Expand All @@ -197,6 +203,7 @@ func (h *Handler) GetConnections(w http.ResponseWriter, req *http.Request, prefO
order := q.Get("order")
search := q.Get("search")
pageSize, _ := strconv.Atoi(q.Get("pagesize"))
filter := q.Get("filter")

if pageSize > 50 {
pageSize = 50
Expand All @@ -211,9 +218,39 @@ func (h *Handler) GetConnections(w http.ResponseWriter, req *http.Request, prefO
order = "updated_at desc"
}

h.log.Debug(fmt.Sprintf("page: %d, page size: %d, search: %s, order: %s", page+1, pageSize, search, order))
err := req.ParseForm()
if err != nil {
h.log.Error(ErrGetConnections(err))
http.Error(w, ErrGetConnections(err).Error(), http.StatusInternalServerError)
return
}

queryParam := struct {
Status []string `json:"status"`
Kind []string `json:"kind"`
}{}

status := q.Get("status")
kind := q.Get("kind")
if status != "" {
err := json.Unmarshal([]byte(status), &queryParam.Status)
if err != nil {
h.log.Error(ErrGetConnections(err))
http.Error(w, ErrGetConnections(err).Error(), http.StatusInternalServerError)
return
}
}

if kind != "" {
err := json.Unmarshal([]byte(kind), &queryParam.Kind)
if err != nil {
h.log.Error(ErrGetConnections(err))
http.Error(w, ErrGetConnections(err).Error(), http.StatusInternalServerError)
return
}
}

connectionsPage, err := provider.GetConnections(req, user.ID, page, pageSize, search, order)
connectionsPage, err := provider.GetConnections(req, user.ID, page, pageSize, search, order, filter, queryParam.Status, queryParam.Kind)
obj := "connections"

if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions server/handlers/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ const (
ErrBulkDeleteEventCode = "1538"
ErrFetchMeshSyncResourcesCode = "1539"
ErrDesignSourceContentCode = "1554"
ErrGetConnectionsCode = "1555"
)

var (
Expand Down Expand Up @@ -536,3 +537,7 @@ func ErrUnsupportedEventStatus(err error, status string) error {
func ErrFetchMeshSyncResources(err error) error {
return errors.New(ErrFetchMeshSyncResourcesCode, errors.Alert, []string{"Error fetching MeshSync resources", "DB might be corrupted"}, []string{err.Error()}, []string{"MeshSync might not be reachable from meshery"}, []string{"Make sure meshery has connectivity to MeshSync", "Try restarting Meshery server"})
}

func ErrGetConnections(err error) error {
return errors.New(ErrGetConnectionsCode, errors.Alert, []string{"Failed to retrieve connections"}, []string{err.Error()}, []string{"Unable to retrieve the connections"}, []string{"Check if the cluster is connected and healthy, you can check it from k8s switcher in header"})
}
6 changes: 6 additions & 0 deletions server/helpers/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,12 @@ paths:
```?page={page-number}``` Default page number is 0
```?pagesize={pagesize}``` Default pagesize is 10
```?filter={filter}``` Filter connections with type or sub_type, eg /api/integrations/connections?filter=type%20platform or /api/integrations/connections?filter=sub_type%20management
```?status={status}``` Status takes array as param to filter connections based on status, eg /api/integrations/connections?status=["connected", "deleted"]
```?kind={kind}``` Kind takes array as param to filter connections based on kind, eg /api/integrations/connections?kind=["meshery", "kubernetes"]
operationId: idGetConnections
responses:
"200":
Expand Down
2 changes: 1 addition & 1 deletion server/models/default_local_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,7 @@ func (l *DefaultLocalProvider) SaveConnection(_ *ConnectionPayload, _ string, _
return nil, ErrLocalProviderSupport
}

func (l *DefaultLocalProvider) GetConnections(_ *http.Request, _ string, _, _ int, _, _ string) (*connections.ConnectionPage, error) {
func (l *DefaultLocalProvider) GetConnections(_ *http.Request, _ string, _, _ int, _, _ string, _ string, _ []string, _ []string) (*connections.ConnectionPage, error) {
return nil, ErrLocalProviderSupport
}
func (l *DefaultLocalProvider) GetConnectionByID(token string, connectionID uuid.UUID, kind string) (*connections.Connection, int, error) {
Expand Down
2 changes: 1 addition & 1 deletion server/models/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ type Provider interface {
ExtensionProxy(req *http.Request) (*ExtensionProxyResponse, error)

SaveConnection(conn *ConnectionPayload, token string, skipTokenCheck bool) (*connections.Connection, error)
GetConnections(req *http.Request, userID string, page, pageSize int, search, order string) (*connections.ConnectionPage, error)
GetConnections(req *http.Request, userID string, page, pageSize int, search, order string, filter string, status []string, kind []string) (*connections.ConnectionPage, error)
GetConnectionByID(token string, connectionID uuid.UUID, kind string) (*connections.Connection, int, error)
GetConnectionsByKind(req *http.Request, userID string, page, pageSize int, search, order, connectionKind string) (*map[string]interface{}, error)
GetConnectionsStatus(req *http.Request, userID string) (*connections.ConnectionsStatusPage, error)
Expand Down
16 changes: 15 additions & 1 deletion server/models/remote_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3666,7 +3666,7 @@ func (l *RemoteProvider) SaveConnection(conn *ConnectionPayload, token string, s
return nil, ErrPost(fmt.Errorf("failed to save the connection"), fmt.Sprint(bdr), resp.StatusCode)
}

func (l *RemoteProvider) GetConnections(req *http.Request, userID string, page, pageSize int, search, order string) (*connections.ConnectionPage, error) {
func (l *RemoteProvider) GetConnections(req *http.Request, userID string, page, pageSize int, search, order string, filter string, status []string, kind []string) (*connections.ConnectionPage, error) {
if !l.Capabilities.IsSupported(PersistConnection) {
logrus.Error("operation not available")
return nil, ErrInvalidCapability("PersistConnection", l.ProviderName)
Expand All @@ -3679,6 +3679,20 @@ func (l *RemoteProvider) GetConnections(req *http.Request, userID string, page,
q.Add("pagesize", strconv.Itoa(pageSize))
q.Add("search", search)
q.Add("order", order)
if filter != "" {
q.Set("filter", filter)
}

if len(status) > 0 {
for _, v := range status {
q.Add("status", v)
}
}
if len(kind) > 0 {
for _, v := range kind {
q.Add("kind", v)
}
}

remoteProviderURL.RawQuery = q.Encode()
logrus.Debugf("Making request to : %s", remoteProviderURL.String())
Expand Down

0 comments on commit c778251

Please sign in to comment.