mirror of
https://github.com/goharbor/harbor.git
synced 2024-11-22 18:25:56 +01:00
Get instance by name.
Signed-off-by: fanjiankong <fanjiankong@tencent.com>
This commit is contained in:
parent
a78ab897d7
commit
3c1c799f0d
@ -734,7 +734,7 @@ paths:
|
||||
$ref: '#/responses/409'
|
||||
'500':
|
||||
$ref: '#/responses/500'
|
||||
/p2p/preheat/instances/{instance_id}:
|
||||
/p2p/preheat/instances/{preheat_instance_name}:
|
||||
get:
|
||||
summary: Get a P2P provider instance
|
||||
description: Get a P2P provider instance
|
||||
@ -743,7 +743,7 @@ paths:
|
||||
operationId: GetInstance
|
||||
parameters:
|
||||
- $ref: '#/parameters/requestId'
|
||||
- $ref: '#/parameters/instanceId'
|
||||
- $ref: '#/parameters/instanceName'
|
||||
responses:
|
||||
'200':
|
||||
description: Success
|
||||
@ -767,7 +767,7 @@ paths:
|
||||
operationId: DeleteInstance
|
||||
parameters:
|
||||
- $ref: '#/parameters/requestId'
|
||||
- $ref: '#/parameters/instanceId'
|
||||
- $ref: '#/parameters/instanceName'
|
||||
responses:
|
||||
'200':
|
||||
description: Instance ID deleted
|
||||
@ -789,7 +789,7 @@ paths:
|
||||
operationId: UpdateInstance
|
||||
parameters:
|
||||
- $ref: '#/parameters/requestId'
|
||||
- $ref: '#/parameters/instanceId'
|
||||
- $ref: '#/parameters/instanceName'
|
||||
- name: propertySet
|
||||
in: body
|
||||
description: The property set to update
|
||||
@ -1017,12 +1017,12 @@ parameters:
|
||||
required: false
|
||||
description: The size of per page
|
||||
default: 10
|
||||
instanceId:
|
||||
name: instance_id
|
||||
instanceName:
|
||||
name: preheat_instance_name
|
||||
in: path
|
||||
description: Instance ID
|
||||
description: Instance Name
|
||||
required: true
|
||||
type: integer
|
||||
type: string
|
||||
preheatPolicyName:
|
||||
name: preheat_policy_name
|
||||
in: path
|
||||
|
@ -62,6 +62,9 @@ type Controller interface {
|
||||
//
|
||||
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.
|
||||
//
|
||||
// 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)
|
||||
}
|
||||
|
||||
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.
|
||||
func (c *controller) CountPolicy(ctx context.Context, query *q.Query) (int64, error) {
|
||||
return c.pManager.Count(ctx, query)
|
||||
|
@ -14,6 +14,7 @@ import (
|
||||
type DAO interface {
|
||||
Create(ctx context.Context, instance *provider.Instance) (int64, 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
|
||||
Delete(ctx context.Context, id int64) 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
|
||||
}
|
||||
|
||||
// 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.
|
||||
func (d *dao) Update(ctx context.Context, instance *provider.Instance, props ...string) error {
|
||||
var o, err = orm.FromContext(ctx)
|
||||
|
@ -6,6 +6,7 @@ import (
|
||||
|
||||
beego_orm "github.com/astaxie/beego/orm"
|
||||
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/q"
|
||||
models "github.com/goharbor/harbor/src/pkg/p2p/preheat/models/provider"
|
||||
@ -63,6 +64,18 @@ func (is *instanceSuite) TestGet() {
|
||||
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() {
|
||||
t := is.T()
|
||||
i, err := is.dao.Get(is.ctx, defaultInstance.ID)
|
||||
|
@ -50,6 +50,10 @@ type Manager interface {
|
||||
//
|
||||
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
|
||||
//
|
||||
// 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)
|
||||
}
|
||||
|
||||
// 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
|
||||
func (dm *manager) Count(ctx context.Context, query *q.Query) (int64, error) {
|
||||
return dm.dao.Count(ctx, query)
|
||||
|
@ -19,6 +19,9 @@ type fakeDao struct {
|
||||
}
|
||||
|
||||
var _ dao.DAO = (*fakeDao)(nil)
|
||||
var lists = []*providerModel.Instance{
|
||||
{Name: "abc"},
|
||||
}
|
||||
|
||||
func (d *fakeDao) Create(ctx context.Context, instance *provider.Instance) (int64, error) {
|
||||
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)
|
||||
}
|
||||
|
||||
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 {
|
||||
var args = d.Called()
|
||||
return args.Error(0)
|
||||
@ -69,6 +81,7 @@ type instanceManagerSuite struct {
|
||||
func (im *instanceManagerSuite) SetupSuite() {
|
||||
im.dao = &fakeDao{}
|
||||
im.manager = &manager{dao: im.dao}
|
||||
im.dao.On("List").Return(lists, nil)
|
||||
}
|
||||
|
||||
func (im *instanceManagerSuite) TestSave() {
|
||||
@ -98,6 +111,13 @@ func (im *instanceManagerSuite) TestGet() {
|
||||
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() {
|
||||
im.dao.On("Count").Return(2, nil)
|
||||
count, err := im.manager.Count(im.ctx, nil)
|
||||
@ -108,12 +128,11 @@ func (im *instanceManagerSuite) TestCount() {
|
||||
func (im *instanceManagerSuite) TestList() {
|
||||
lists := []*providerModel.Instance{
|
||||
{Name: "abc"},
|
||||
{Name: "def"},
|
||||
}
|
||||
im.dao.On("List").Return(lists, nil)
|
||||
res, err := im.manager.List(im.ctx, nil)
|
||||
assert.Nil(im.T(), err)
|
||||
assert.Len(im.T(), res, 2)
|
||||
assert.Len(im.T(), res, 1)
|
||||
assert.Equal(im.T(), lists, res)
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
||||
@ -75,6 +75,29 @@ func (_m *FakeManager) Get(ctx context.Context, id int64) (*provider.Instance, e
|
||||
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
|
||||
func (_m *FakeManager) List(ctx context.Context, query *q.Query) ([]*provider.Instance, error) {
|
||||
ret := _m.Called(ctx, query)
|
||||
|
Loading…
Reference in New Issue
Block a user