Skip to content

Commit

Permalink
Api to get resources from one or multiple catalogs
Browse files Browse the repository at this point in the history
This patch adds a catalogs params to query api and
which takes one or multiple catalogs name

Signed-off-by: Shiv Verma <[email protected]>
  • Loading branch information
pratap0007 committed Apr 12, 2021
1 parent 302ad8a commit a6ecfa2
Show file tree
Hide file tree
Showing 26 changed files with 209 additions and 67 deletions.
6 changes: 5 additions & 1 deletion api/design/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ var _ = Service("resource", func() {

// Will be deprecated, moved to v1
Method("Query", func() {
Description("Find resources by a combination of name, kind and tags")
Description("Find resources by a combination of name, kind , catalog and tags")
Payload(func() {
Attribute("name", String, "Name of resource", func() {
Default("")
Example("name", "buildah")
})
Attribute("catalogs", ArrayOf(String), "Catalogs of resource to filter by", func() {
Example([]string{"tekton", "openshift"})
})
Attribute("kinds", ArrayOf(String), "Kinds of resource to filter by", func() {
Example([]string{"task", "pipelines"})
})
Expand All @@ -60,6 +63,7 @@ var _ = Service("resource", func() {
GET("/query")

Param("name")
Param("catalogs")
Param("kinds")
Param("tags")
Param("limit")
Expand Down
27 changes: 16 additions & 11 deletions api/gen/http/cli/hub/cli.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api/gen/http/openapi.json

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion api/gen/http/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ paths:
tags:
- resource
summary: Query resource
description: Find resources by a combination of name, kind and tags
description: Find resources by a combination of name, kind , catalog and tags
operationId: resource#Query
parameters:
- name: name
Expand All @@ -168,6 +168,14 @@ paths:
required: false
type: string
default: ""
- name: catalogs
in: query
description: Catalogs of resource to filter by
required: false
type: array
items:
type: string
collectionFormat: multi
- name: kinds
in: query
description: Kinds of resource to filter by
Expand Down
2 changes: 1 addition & 1 deletion api/gen/http/openapi3.json

Large diffs are not rendered by default.

32 changes: 24 additions & 8 deletions api/gen/http/openapi3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ paths:
tags:
- resource
summary: Query resource
description: Find resources by a combination of name, kind and tags
description: Find resources by a combination of name, kind , catalog and tags
operationId: resource#Query
parameters:
- name: name
Expand All @@ -277,6 +277,22 @@ paths:
default: ""
example: buildah
example: buildah
- name: catalogs
in: query
description: Catalogs of resource to filter by
allowEmptyValue: true
schema:
type: array
items:
type: string
example: Et at.
description: Catalogs of resource to filter by
example:
- tekton
- openshift
example:
- tekton
- openshift
- name: kinds
in: query
description: Kinds of resource to filter by
Expand All @@ -285,7 +301,7 @@ paths:
type: array
items:
type: string
example: Et at.
example: Aperiam tempore animi iure eum et et.
description: Kinds of resource to filter by
example:
- task
Expand All @@ -301,7 +317,7 @@ paths:
type: array
items:
type: string
example: Aperiam tempore animi iure eum et et.
example: Pariatur corrupti.
description: Tags associated with a resource to filter by
example:
- image
Expand All @@ -327,11 +343,11 @@ paths:
type: string
description: Strategy used to find matching resources
default: contains
example: exact
example: contains
enum:
- exact
- contains
example: contains
example: exact
responses:
"200":
description: OK response.
Expand Down Expand Up @@ -448,11 +464,11 @@ paths:
schema:
type: string
description: kind of resource
example: task
example: pipeline
enum:
- task
- pipeline
example: pipeline
example: task
- name: name
in: path
description: Name of resource
Expand Down Expand Up @@ -546,7 +562,7 @@ paths:
enum:
- task
- pipeline
example: pipeline
example: task
- name: name
in: path
description: name of resource
Expand Down
12 changes: 11 additions & 1 deletion api/gen/http/resource/client/cli.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions api/gen/http/resource/client/encode_decode.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 9 additions & 7 deletions api/gen/http/resource/server/encode_decode.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion api/gen/http/resource/server/types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion api/gen/resource/service.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/pkg/service/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (s *service) Query(ctx context.Context, p *resource.QueryPayload) (*resourc
Log: s.Logger(ctx),
Name: p.Name,
Kinds: p.Kinds,
Catalogs: p.Catalogs,
Tags: p.Tags,
Limit: p.Limit,
Match: p.Match,
Expand Down
17 changes: 16 additions & 1 deletion api/pkg/shared/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Request struct {
Name string
Match string
Kinds []string
Catalogs []string
Tags []string
Limit uint
Version string
Expand All @@ -46,7 +47,7 @@ var (

// Query resources based on name, kind, tags.
// Match is the type of search: 'exact' or 'contains'
// Fields: name, []kinds, []Tags, Match, Limit
// Fields: name, []kinds,[]Catalogs []Tags, Match, Limit
func (r *Request) Query() ([]model.Resource, error) {

// Validate the kinds passed are supported by Hub
Expand All @@ -64,6 +65,7 @@ func (r *Request) Query() ([]model.Resource, error) {
r.Db = r.Db.Select("DISTINCT(resources.id), resources.*").Scopes(
filterByTags(r.Tags),
filterByKinds(r.Kinds),
filterByCatalogs(r.Catalogs),
filterResourceName(r.Match, r.Name),
withResourceDetails,
).Limit(int(r.Limit))
Expand Down Expand Up @@ -314,6 +316,19 @@ func filterResourceName(match, name string) func(db *gorm.DB) *gorm.DB {
}
}

func filterByCatalogs(catalogs []string) func(db *gorm.DB) *gorm.DB {
if len(catalogs) == 0 {
return noop
}

catalogs = lower(catalogs)
return func(db *gorm.DB) *gorm.DB {
return db.Model(&model.Resource{}).
Joins("JOIN catalogs as ct on ct.id = resources.catalog_id").
Where("lower(ct.name) in (?)", catalogs)
}
}

func noop(db *gorm.DB) *gorm.DB {
return db
}
Expand Down
20 changes: 20 additions & 0 deletions api/pkg/shared/resource/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,26 @@ func TestQuery_ByTags(t *testing.T) {
assert.Equal(t, 1, len(res))
}

func TestQuery_ByCatalogs(t *testing.T) {
tc := testutils.Setup(t)
testutils.LoadFixtures(t, tc.FixturePath())

req := Request{
Db: tc.DB(),
Log: tc.Logger("resource"),
Name: "",
Kinds: []string{},
Catalogs: []string{"catalog-community"},
Limit: 100,
}

res, err := req.Query()
assert.NoError(t, err)

assert.Equal(t, 1, len(res))
assert.Equal(t,"catalog-community",res[0].Catalog.Name)
}

func TestQuery_ByNameAndKind(t *testing.T) {
tc := testutils.Setup(t)
testutils.LoadFixtures(t, tc.FixturePath())
Expand Down
Loading

0 comments on commit a6ecfa2

Please sign in to comment.