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

add an new api for listing all projects #240

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion docs/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,44 @@ paths:
summary: health check return version and revision
operationId: HealthCheck
consumes:
- '*/*'
produces:
- application/json
- text/yaml
responses:
'200':
description: ''
schema:
$ref: '#/definitions/DocHealthCheck'
'/v1/project':
get:
summary: list projects
operationId: Project
parameters:
- name: project
in: query
required: false
type: string
- name: limit
in: query
description: pagination
type: string
- name: offset
in: query
description: pagination
type: string
consumes:
- '*/*'
produces:
- application/json
- text/yaml
responses:
'200':
description: ''
schema:
$ref: '#/definitions/DocHealthCheck'
$ref: '#/definitions/DocResponseGetProject'
'304':
description: empty body
'/v1/{project}/kie/kv':
get:
summary: list key values by labels and key
Expand Down Expand Up @@ -285,6 +313,16 @@ definitions:
format: int64
version:
type: string
DocResponseGetProject:
type: object
properties:
total:
type: integer
format: int64
data:
type: array
items:
type: string
DocResponseGetKey:
type: object
properties:
Expand Down
1 change: 1 addition & 0 deletions pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const (
QueryParamURLPath = "urlPath"
QueryParamUserAgent = "userAgent"
QueryParamOverride = "override"
QueryParameterProject = "project"
)

//http headers
Expand Down
13 changes: 13 additions & 0 deletions pkg/model/db_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@ type GetKVRequest struct {
ID string `json:"id,omitempty" bson:"id,omitempty" yaml:"id,omitempty" swag:"string" validate:"uuid"`
}

// ListProjectRequest contains project list request params
type ListProjectRequest struct {
Domain string `json:"domaListProjectRequestin,omitempty" yaml:"domain,omitempty" validate:"min=1,max=256,commonName"` //redundant
Project string `json:"project,omitempty" yaml:"project,omitempty"`
Offset int64 `validate:"min=0"`
Limit int64 `validate:"min=0,max=100"`
}

//ProjectDoc is database struct to store projects
type ProjectDoc struct {
Project string `json:"project,omitempty" bson:"project,omitempty" yaml:"project,omitempty" validate:"min=1,max=256,commonName"`
}

// ListKVRequest contains kv list request params
type ListKVRequest struct {
Project string `json:"project,omitempty" yaml:"project,omitempty" validate:"min=1,max=256,commonName"`
Expand Down
7 changes: 7 additions & 0 deletions pkg/model/project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package model

//ProjectResponse represents the project list
type ProjectResponse struct {
Total int `json:"total"`
Data []*ProjectDoc `json:"data"`
}
6 changes: 6 additions & 0 deletions server/datasource/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type Broker interface {
GetHistoryDao() HistoryDao
GetTrackDao() TrackDao
GetKVDao() KVDao
GetProjectDao() ProjectDao
}

func GetBroker() Broker {
Expand All @@ -87,6 +88,11 @@ type KVDao interface {
Total(ctx context.Context, project, domain string) (int64, error)
}

type ProjectDao interface {
List(ctx context.Context, domain string, options ...FindOption) (*model.ProjectResponse, error)
Total(ctx context.Context, domain string) (int64, error)
}

//HistoryDao provide api of History entity
type HistoryDao interface {
AddHistory(ctx context.Context, kv *model.KVDoc) error
Expand Down
4 changes: 4 additions & 0 deletions server/datasource/etcd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package etcd
import (
"crypto/tls"
"fmt"
"github.com/apache/servicecomb-kie/server/datasource/etcd/project"

"github.com/go-chassis/cari/db"
dconfig "github.com/go-chassis/cari/db/config"
Expand Down Expand Up @@ -69,6 +70,9 @@ func (*Broker) GetHistoryDao() datasource.HistoryDao {
func (*Broker) GetTrackDao() datasource.TrackDao {
return &track.Dao{}
}
func (*Broker) GetProjectDao() datasource.ProjectDao {
return &project.Dao{}
}

func init() {
datasource.RegisterPlugin("etcd", NewFrom)
Expand Down
41 changes: 41 additions & 0 deletions server/datasource/etcd/project/project_dao.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 project

import (
"context"

"github.com/apache/servicecomb-kie/pkg/model"
"github.com/apache/servicecomb-kie/server/datasource"
)

//Dao operate data in etcd
type Dao struct {
}

//Total get projects total counts by domain
func (s *Dao) Total(ctx context.Context, domain string) (int64, error) {
// TODO etcd needs to be done
return 0, nil
}

//List get projects list by domain and name
func (s *Dao) List(ctx context.Context, domain string, options ...datasource.FindOption) (*model.ProjectResponse, error) {
// TODO etcd needs to be done
nisainan marked this conversation as resolved.
Show resolved Hide resolved
return nil, nil
}
22 changes: 12 additions & 10 deletions server/datasource/mongo/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,23 @@ import (
"context"
"crypto/tls"
"fmt"

"github.com/go-chassis/cari/db"
dconfig "github.com/go-chassis/cari/db/config"
dmongo "github.com/go-chassis/cari/db/mongo"
"github.com/go-chassis/openlog"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/bsonx"

"github.com/apache/servicecomb-kie/server/config"
"github.com/apache/servicecomb-kie/server/datasource"
"github.com/apache/servicecomb-kie/server/datasource/mongo/counter"
"github.com/apache/servicecomb-kie/server/datasource/mongo/history"
"github.com/apache/servicecomb-kie/server/datasource/mongo/kv"
"github.com/apache/servicecomb-kie/server/datasource/mongo/model"
"github.com/apache/servicecomb-kie/server/datasource/mongo/project"
"github.com/apache/servicecomb-kie/server/datasource/mongo/track"
"github.com/apache/servicecomb-kie/server/datasource/tlsutil"
"github.com/go-chassis/cari/db"
dconfig "github.com/go-chassis/cari/db/config"
dmongo "github.com/go-chassis/cari/db/mongo"
"github.com/go-chassis/openlog"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/bsonx"
)

type Broker struct {
Expand Down Expand Up @@ -84,6 +83,9 @@ func (*Broker) GetHistoryDao() datasource.HistoryDao {
func (*Broker) GetTrackDao() datasource.TrackDao {
return &track.Dao{}
}
func (*Broker) GetProjectDao() datasource.ProjectDao {
return &project.Dao{}
}

func ensureDB() error {
err := ensureRevisionCounter()
Expand Down
104 changes: 104 additions & 0 deletions server/datasource/mongo/project/project_dao.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 project

import (
"context"
"fmt"
"github.com/apache/servicecomb-kie/pkg/model"
"github.com/apache/servicecomb-kie/server/datasource"
mmodel "github.com/apache/servicecomb-kie/server/datasource/mongo/model"
"github.com/go-chassis/cari/db/mongo"
"github.com/go-chassis/openlog"
"go.mongodb.org/mongo-driver/bson"
"strings"
)

const (
MsgFindKvFailed = "find kv failed, deadline exceeded"
FmtErrFindKvFailed = "can not find kv in %s"
)

//Dao operate data in mongodb
type Dao struct {
}

//Total get projects total counts by domain
func (s *Dao) Total(ctx context.Context, domain string) (int64, error) {
collection := mongo.GetClient().GetDB().Collection(mmodel.CollectionKV)
filter := bson.M{"domain": domain}
total, err := collection.CountDocuments(ctx, filter)
if err != nil {
openlog.Error("find total number: " + err.Error())
return 0, err
}
return total, err
}

//List get projects list by domain and name
func (s *Dao) List(ctx context.Context, domain string, options ...datasource.FindOption) (*model.ProjectResponse, error) {
opts := datasource.NewDefaultFindOpts()
for _, o := range options {
o(&opts)
}
data, total, err := findProjects(ctx, domain, opts)
if err != nil {
return nil, err
}
result := &model.ProjectResponse{
Data: []*model.ProjectDoc{},
}
for _, value := range data {
curKV := &model.ProjectDoc{Project: value.(string)}
result.Data = append(result.Data, curKV)
}
result.Total = total
return result, nil
}

func findProjects(ctx context.Context, domain string, opts datasource.FindOptions) ([]interface{}, int, error) {
collection := mongo.GetClient().GetDB().Collection(mmodel.CollectionKV)
ctx, cancel := context.WithTimeout(ctx, opts.Timeout)
defer cancel()

filter := bson.M{"domain": domain}
if len(strings.TrimSpace(opts.Project)) > 0 {
filter["project"] = opts.Project
}
result, err := collection.Distinct(ctx, "project", filter)
if err != nil {
if err.Error() == context.DeadlineExceeded.Error() {
openlog.Error(MsgFindKvFailed, openlog.WithTags(openlog.Tags{
"timeout": opts.Timeout,
}))
return nil, 0, fmt.Errorf(FmtErrFindKvFailed, opts.Timeout)
}
return nil, 0, err
}
curTotal := len(result)
offset := opts.Offset
limit := offset + opts.Limit
if offset > int64(curTotal) {
offset = int64(curTotal)
limit = int64(curTotal)
} else if limit > int64(curTotal) {
limit = int64(curTotal)
}
result = result[offset:limit]
return result, len(result), err
}
8 changes: 8 additions & 0 deletions server/datasource/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type WriteOptions struct {
type FindOptions struct {
ExactLabels bool
Status string
Project string
Depth int
ID string
Key string
Expand All @@ -86,6 +87,13 @@ type WriteOption func(*WriteOptions)
//FindOption is functional option to find key value
type FindOption func(*FindOptions)

//WithProject find by project
func WithProject(project string) FindOption {
return func(o *FindOptions) {
o.Project = project
}
}

// WithSync indicates that the synchronization function is on
func WithSync(enabled bool) WriteOption {
return func(o *WriteOptions) {
Expand Down
8 changes: 7 additions & 1 deletion server/resource/v1/doc_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ var (
ParamType: goRestful.QueryParameterKind,
Desc: "pagination",
}
//polling data
// DocQuerySessionIDParameters polling data
DocQuerySessionIDParameters = &restful.Parameters{
DataType: "string",
Name: common.QueryParamSessionID,
Expand All @@ -152,6 +152,12 @@ var (
ParamType: goRestful.QueryParameterKind,
Desc: "user agent of the call",
}
DocQueryProjectParameters = &restful.Parameters{
DataType: "string",
Name: common.QueryParameterProject,
ParamType: goRestful.QueryParameterKind,
Desc: "project name of the call",
}
)

//swagger doc path params
Expand Down
Loading