Merge pull request #12411 from kofj/instance_by_name

Get instance by name.
This commit is contained in:
Steven Zou 2020-07-07 23:20:01 +08:00 committed by GitHub
commit b56a49efe2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 100 additions and 11 deletions

View File

@ -734,7 +734,7 @@ paths:
$ref: '#/responses/409' $ref: '#/responses/409'
'500': '500':
$ref: '#/responses/500' $ref: '#/responses/500'
/p2p/preheat/instances/{instance_id}: /p2p/preheat/instances/{preheat_instance_name}:
get: get:
summary: Get a P2P provider instance summary: Get a P2P provider instance
description: Get a P2P provider instance description: Get a P2P provider instance
@ -743,7 +743,7 @@ paths:
operationId: GetInstance operationId: GetInstance
parameters: parameters:
- $ref: '#/parameters/requestId' - $ref: '#/parameters/requestId'
- $ref: '#/parameters/instanceId' - $ref: '#/parameters/instanceName'
responses: responses:
'200': '200':
description: Success description: Success
@ -767,7 +767,7 @@ paths:
operationId: DeleteInstance operationId: DeleteInstance
parameters: parameters:
- $ref: '#/parameters/requestId' - $ref: '#/parameters/requestId'
- $ref: '#/parameters/instanceId' - $ref: '#/parameters/instanceName'
responses: responses:
'200': '200':
description: Instance ID deleted description: Instance ID deleted
@ -789,7 +789,7 @@ paths:
operationId: UpdateInstance operationId: UpdateInstance
parameters: parameters:
- $ref: '#/parameters/requestId' - $ref: '#/parameters/requestId'
- $ref: '#/parameters/instanceId' - $ref: '#/parameters/instanceName'
- name: propertySet - name: propertySet
in: body in: body
description: The property set to update description: The property set to update
@ -1017,12 +1017,12 @@ parameters:
required: false required: false
description: The size of per page description: The size of per page
default: 10 default: 10
instanceId: instanceName:
name: instance_id name: preheat_instance_name
in: path in: path
description: Instance ID description: Instance Name
required: true required: true
type: integer type: string
preheatPolicyName: preheatPolicyName:
name: preheat_policy_name name: preheat_policy_name
in: path in: path

View File

@ -62,6 +62,9 @@ type Controller interface {
// //
GetInstance(ctx context.Context, id int64) (*providerModels.Instance, error) GetInstance(ctx context.Context, id int64) (*providerModels.Instance, error)
// GetInstance the metadata of the specified instance
GetInstanceByName(ctx context.Context, name string) (*providerModels.Instance, error)
// Create a new instance for the specified provider. // Create a new instance for the specified provider.
// //
// If succeed, the ID of the instance will be returned. // If succeed, the ID of the instance will be returned.
@ -190,6 +193,10 @@ func (c *controller) GetInstance(ctx context.Context, id int64) (*providerModels
return c.iManager.Get(ctx, id) return c.iManager.Get(ctx, id)
} }
func (c *controller) GetInstanceByName(ctx context.Context, name string) (*providerModels.Instance, error) {
return c.iManager.GetByName(ctx, name)
}
// CountPolicy returns the total count of the policy. // CountPolicy returns the total count of the policy.
func (c *controller) CountPolicy(ctx context.Context, query *q.Query) (int64, error) { func (c *controller) CountPolicy(ctx context.Context, query *q.Query) (int64, error) {
return c.pManager.Count(ctx, query) return c.pManager.Count(ctx, query)

View File

@ -14,6 +14,7 @@ import (
type DAO interface { type DAO interface {
Create(ctx context.Context, instance *provider.Instance) (int64, error) Create(ctx context.Context, instance *provider.Instance) (int64, error)
Get(ctx context.Context, id int64) (*provider.Instance, error) Get(ctx context.Context, id int64) (*provider.Instance, error)
GetByName(ctx context.Context, name string) (*provider.Instance, error)
Update(ctx context.Context, instance *provider.Instance, props ...string) error Update(ctx context.Context, instance *provider.Instance, props ...string) error
Delete(ctx context.Context, id int64) error Delete(ctx context.Context, id int64) error
Count(ctx context.Context, query *q.Query) (total int64, err error) Count(ctx context.Context, query *q.Query) (total int64, err error)
@ -60,6 +61,23 @@ func (d *dao) Get(ctx context.Context, id int64) (*provider.Instance, error) {
return &di, err return &di, err
} }
// Get gets instance from db by name.
func (d *dao) GetByName(ctx context.Context, name string) (instance *provider.Instance, err error) {
o, err := orm.FromContext(ctx)
if err != nil {
return nil, err
}
instance = &provider.Instance{Name: name}
if err = o.Read(instance, "Name"); err != nil {
if e := orm.AsNotFoundError(err, "instance %s not found", name); e != nil {
err = e
}
return nil, err
}
return
}
// Update updates distribution instance. // Update updates distribution instance.
func (d *dao) Update(ctx context.Context, instance *provider.Instance, props ...string) error { func (d *dao) Update(ctx context.Context, instance *provider.Instance, props ...string) error {
var o, err = orm.FromContext(ctx) var o, err = orm.FromContext(ctx)

View File

@ -6,6 +6,7 @@ import (
beego_orm "github.com/astaxie/beego/orm" beego_orm "github.com/astaxie/beego/orm"
common_dao "github.com/goharbor/harbor/src/common/dao" common_dao "github.com/goharbor/harbor/src/common/dao"
"github.com/goharbor/harbor/src/lib/errors"
"github.com/goharbor/harbor/src/lib/orm" "github.com/goharbor/harbor/src/lib/orm"
"github.com/goharbor/harbor/src/lib/q" "github.com/goharbor/harbor/src/lib/q"
models "github.com/goharbor/harbor/src/pkg/p2p/preheat/models/provider" models "github.com/goharbor/harbor/src/pkg/p2p/preheat/models/provider"
@ -63,6 +64,18 @@ func (is *instanceSuite) TestGet() {
assert.Nil(t, i) assert.Nil(t, i)
} }
// TestGetByName tests get a instance by name.
func (is *instanceSuite) TestGetByName() {
instance, err := is.dao.GetByName(is.ctx, defaultInstance.Name)
is.Require().Nil(err)
is.Require().NotNil(instance)
is.Equal(defaultInstance.Name, instance.Name, "get a default instance")
// not found
_, err = is.dao.GetByName(is.ctx, "default-instance")
is.Require().NotNil(err)
is.True(errors.IsErr(err, errors.NotFoundCode))
}
func (is *instanceSuite) TestUpdate() { func (is *instanceSuite) TestUpdate() {
t := is.T() t := is.T()
i, err := is.dao.Get(is.ctx, defaultInstance.ID) i, err := is.dao.Get(is.ctx, defaultInstance.ID)

View File

@ -50,6 +50,10 @@ type Manager interface {
// //
Get(ctx context.Context, id int64) (*provider.Instance, error) Get(ctx context.Context, id int64) (*provider.Instance, error)
// GetByName gets the repository specified by name
// name string : the global unique name of the instance
GetByName(ctx context.Context, name string) (*provider.Instance, error)
// Count the instances by the param // Count the instances by the param
// //
// query *q.Query : the query params // query *q.Query : the query params
@ -100,6 +104,11 @@ func (dm *manager) Get(ctx context.Context, id int64) (*provider.Instance, error
return dm.dao.Get(ctx, id) return dm.dao.Get(ctx, id)
} }
// Get implements @Manager.GetByName
func (dm *manager) GetByName(ctx context.Context, name string) (*provider.Instance, error) {
return dm.dao.GetByName(ctx, name)
}
// Count implements @Manager.Count // Count implements @Manager.Count
func (dm *manager) Count(ctx context.Context, query *q.Query) (int64, error) { func (dm *manager) Count(ctx context.Context, query *q.Query) (int64, error) {
return dm.dao.Count(ctx, query) return dm.dao.Count(ctx, query)

View File

@ -19,6 +19,9 @@ type fakeDao struct {
} }
var _ dao.DAO = (*fakeDao)(nil) var _ dao.DAO = (*fakeDao)(nil)
var lists = []*providerModel.Instance{
{Name: "abc"},
}
func (d *fakeDao) Create(ctx context.Context, instance *provider.Instance) (int64, error) { func (d *fakeDao) Create(ctx context.Context, instance *provider.Instance) (int64, error) {
var args = d.Called() var args = d.Called()
@ -34,6 +37,15 @@ func (d *fakeDao) Get(ctx context.Context, id int64) (*provider.Instance, error)
return instance, args.Error(1) return instance, args.Error(1)
} }
func (d *fakeDao) GetByName(ctx context.Context, name string) (*provider.Instance, error) {
var args = d.Called()
var instance *provider.Instance
if args.Get(0) != nil {
instance = args.Get(0).(*provider.Instance)
}
return instance, args.Error(1)
}
func (d *fakeDao) Update(ctx context.Context, instance *provider.Instance, props ...string) error { func (d *fakeDao) Update(ctx context.Context, instance *provider.Instance, props ...string) error {
var args = d.Called() var args = d.Called()
return args.Error(0) return args.Error(0)
@ -69,6 +81,7 @@ type instanceManagerSuite struct {
func (im *instanceManagerSuite) SetupSuite() { func (im *instanceManagerSuite) SetupSuite() {
im.dao = &fakeDao{} im.dao = &fakeDao{}
im.manager = &manager{dao: im.dao} im.manager = &manager{dao: im.dao}
im.dao.On("List").Return(lists, nil)
} }
func (im *instanceManagerSuite) TestSave() { func (im *instanceManagerSuite) TestSave() {
@ -98,6 +111,13 @@ func (im *instanceManagerSuite) TestGet() {
im.Require().Equal(ins, res) im.Require().Equal(ins, res)
} }
func (im *instanceManagerSuite) TestGetByName() {
im.dao.On("GetByName").Return(lists[0], nil)
res, err := im.manager.GetByName(im.ctx, "abc")
im.Require().Nil(err)
im.Require().Equal(lists[0], res)
}
func (im *instanceManagerSuite) TestCount() { func (im *instanceManagerSuite) TestCount() {
im.dao.On("Count").Return(2, nil) im.dao.On("Count").Return(2, nil)
count, err := im.manager.Count(im.ctx, nil) count, err := im.manager.Count(im.ctx, nil)
@ -108,12 +128,11 @@ func (im *instanceManagerSuite) TestCount() {
func (im *instanceManagerSuite) TestList() { func (im *instanceManagerSuite) TestList() {
lists := []*providerModel.Instance{ lists := []*providerModel.Instance{
{Name: "abc"}, {Name: "abc"},
{Name: "def"},
} }
im.dao.On("List").Return(lists, nil) im.dao.On("List").Return(lists, nil)
res, err := im.manager.List(im.ctx, nil) res, err := im.manager.List(im.ctx, nil)
assert.Nil(im.T(), err) assert.Nil(im.T(), err)
assert.Len(im.T(), res, 2) assert.Len(im.T(), res, 1)
assert.Equal(im.T(), lists, res) assert.Equal(im.T(), lists, res)
} }

View File

@ -1,4 +1,4 @@
// Code generated by mockery v2.0.3. DO NOT EDIT. // Code generated by mockery v1.0.0. DO NOT EDIT.
package instance package instance
@ -75,6 +75,29 @@ func (_m *FakeManager) Get(ctx context.Context, id int64) (*provider.Instance, e
return r0, r1 return r0, r1
} }
// GetByName provides a mock function with given fields: ctx, name
func (_m *FakeManager) GetByName(ctx context.Context, name string) (*provider.Instance, error) {
ret := _m.Called(ctx, name)
var r0 *provider.Instance
if rf, ok := ret.Get(0).(func(context.Context, string) *provider.Instance); ok {
r0 = rf(ctx, name)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*provider.Instance)
}
}
var r1 error
if rf, ok := ret.Get(1).(func(context.Context, string) error); ok {
r1 = rf(ctx, name)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// List provides a mock function with given fields: ctx, query // List provides a mock function with given fields: ctx, query
func (_m *FakeManager) List(ctx context.Context, query *q.Query) ([]*provider.Instance, error) { func (_m *FakeManager) List(ctx context.Context, query *q.Query) ([]*provider.Instance, error) {
ret := _m.Called(ctx, query) ret := _m.Called(ctx, query)