2019-12-20 02:24:57 +01:00
|
|
|
// Copyright Project Harbor Authors
|
|
|
|
//
|
|
|
|
// Licensed 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 repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-02-07 02:11:54 +01:00
|
|
|
"github.com/goharbor/harbor/src/api/artifact"
|
2019-12-20 02:24:57 +01:00
|
|
|
"github.com/goharbor/harbor/src/common/models"
|
2020-01-21 07:11:56 +01:00
|
|
|
"github.com/goharbor/harbor/src/common/utils"
|
2019-12-20 02:24:57 +01:00
|
|
|
ierror "github.com/goharbor/harbor/src/internal/error"
|
2020-01-21 07:11:56 +01:00
|
|
|
"github.com/goharbor/harbor/src/pkg/project"
|
2019-12-20 02:24:57 +01:00
|
|
|
"github.com/goharbor/harbor/src/pkg/q"
|
|
|
|
"github.com/goharbor/harbor/src/pkg/repository"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// Ctl is a global repository controller instance
|
2020-01-21 07:11:56 +01:00
|
|
|
Ctl = NewController()
|
2019-12-20 02:24:57 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Controller defines the operations related with repositories
|
|
|
|
type Controller interface {
|
2020-01-21 07:11:56 +01:00
|
|
|
// Ensure the repository specified by the "name" exists, creates it if it doesn't exist.
|
|
|
|
// The "name" should contain the namespace part. The "created" will be set as true
|
|
|
|
// when the repository is created
|
|
|
|
Ensure(ctx context.Context, name string) (created bool, id int64, err error)
|
2020-02-18 08:51:38 +01:00
|
|
|
// Count returns the total count of repositories according to the query
|
|
|
|
Count(ctx context.Context, query *q.Query) (total int64, err error)
|
2020-01-22 11:28:08 +01:00
|
|
|
// List repositories according to the query
|
2020-02-18 08:51:38 +01:00
|
|
|
List(ctx context.Context, query *q.Query) (repositories []*models.RepoRecord, err error)
|
2019-12-20 02:24:57 +01:00
|
|
|
// Get the repository specified by ID
|
|
|
|
Get(ctx context.Context, id int64) (repository *models.RepoRecord, err error)
|
2020-01-22 11:28:08 +01:00
|
|
|
// GetByName gets the repository specified by name
|
|
|
|
GetByName(ctx context.Context, name string) (repository *models.RepoRecord, err error)
|
2020-02-07 02:11:54 +01:00
|
|
|
// Delete the repository specified by ID
|
|
|
|
Delete(ctx context.Context, id int64) (err error)
|
2020-02-18 08:51:38 +01:00
|
|
|
// Update the repository. Specify the properties or all properties will be updated
|
|
|
|
Update(ctx context.Context, repository *models.RepoRecord, properties ...string) (err error)
|
2019-12-20 02:24:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewController creates an instance of the default repository controller
|
2020-01-21 07:11:56 +01:00
|
|
|
func NewController() Controller {
|
2019-12-20 02:24:57 +01:00
|
|
|
return &controller{
|
2020-01-21 07:11:56 +01:00
|
|
|
proMgr: project.Mgr,
|
|
|
|
repoMgr: repository.Mgr,
|
2020-02-07 02:11:54 +01:00
|
|
|
artCtl: artifact.Ctl,
|
2019-12-20 02:24:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type controller struct {
|
2020-01-21 07:11:56 +01:00
|
|
|
proMgr project.Manager
|
2019-12-20 02:24:57 +01:00
|
|
|
repoMgr repository.Manager
|
2020-02-07 02:11:54 +01:00
|
|
|
artCtl artifact.Controller
|
2019-12-20 02:24:57 +01:00
|
|
|
}
|
|
|
|
|
2020-01-21 07:11:56 +01:00
|
|
|
func (c *controller) Ensure(ctx context.Context, name string) (bool, int64, error) {
|
2019-12-20 02:24:57 +01:00
|
|
|
query := &q.Query{
|
|
|
|
Keywords: map[string]interface{}{
|
|
|
|
"name": name,
|
|
|
|
},
|
|
|
|
}
|
2020-02-18 08:51:38 +01:00
|
|
|
repositories, err := c.repoMgr.List(ctx, query)
|
2019-12-20 02:24:57 +01:00
|
|
|
if err != nil {
|
|
|
|
return false, 0, err
|
|
|
|
}
|
|
|
|
// the repository already exists, return directly
|
|
|
|
if len(repositories) > 0 {
|
|
|
|
return false, repositories[0].RepositoryID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// the repository doesn't exist, create it first
|
2020-01-21 07:11:56 +01:00
|
|
|
projectName, _ := utils.ParseRepository(name)
|
|
|
|
project, err := c.proMgr.Get(projectName)
|
|
|
|
if err != nil {
|
|
|
|
return false, 0, err
|
|
|
|
}
|
2019-12-20 02:24:57 +01:00
|
|
|
id, err := c.repoMgr.Create(ctx, &models.RepoRecord{
|
2020-01-21 07:11:56 +01:00
|
|
|
ProjectID: project.ProjectID,
|
2019-12-20 02:24:57 +01:00
|
|
|
Name: name,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
// if got conflict error, try to get again
|
|
|
|
if ierror.IsConflictErr(err) {
|
2020-02-18 08:51:38 +01:00
|
|
|
repositories, err = c.repoMgr.List(ctx, query)
|
2019-12-20 02:24:57 +01:00
|
|
|
if err != nil {
|
|
|
|
return false, 0, err
|
|
|
|
}
|
|
|
|
if len(repositories) > 0 {
|
|
|
|
return false, repositories[0].RepositoryID, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, 0, err
|
|
|
|
}
|
|
|
|
return true, id, nil
|
|
|
|
}
|
|
|
|
|
2020-02-18 08:51:38 +01:00
|
|
|
func (c *controller) Count(ctx context.Context, query *q.Query) (int64, error) {
|
|
|
|
return c.repoMgr.Count(ctx, query)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *controller) List(ctx context.Context, query *q.Query) ([]*models.RepoRecord, error) {
|
2020-01-22 11:28:08 +01:00
|
|
|
return c.repoMgr.List(ctx, query)
|
|
|
|
}
|
|
|
|
|
2019-12-20 02:24:57 +01:00
|
|
|
func (c *controller) Get(ctx context.Context, id int64) (*models.RepoRecord, error) {
|
|
|
|
return c.repoMgr.Get(ctx, id)
|
|
|
|
}
|
2020-01-22 11:28:08 +01:00
|
|
|
|
|
|
|
func (c *controller) GetByName(ctx context.Context, name string) (*models.RepoRecord, error) {
|
|
|
|
return c.repoMgr.GetByName(ctx, name)
|
|
|
|
}
|
2020-02-07 02:11:54 +01:00
|
|
|
|
|
|
|
func (c *controller) Delete(ctx context.Context, id int64) error {
|
|
|
|
// TODO auth
|
|
|
|
// TODO how to make sure the logic included by middlewares(immutable, readonly, quota, etc)
|
|
|
|
// TODO is covered when deleting the artifacts of the repository
|
2020-02-18 08:51:38 +01:00
|
|
|
artifacts, err := c.artCtl.List(ctx, &q.Query{
|
2020-02-07 02:11:54 +01:00
|
|
|
Keywords: map[string]interface{}{
|
|
|
|
"RepositoryID": id,
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, artifact := range artifacts {
|
|
|
|
if err = c.artCtl.Delete(ctx, artifact.ID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return c.repoMgr.Delete(ctx, id)
|
|
|
|
|
|
|
|
// TODO fire event
|
|
|
|
}
|
2020-02-18 08:51:38 +01:00
|
|
|
|
|
|
|
func (c *controller) Update(ctx context.Context, repository *models.RepoRecord, properties ...string) error {
|
|
|
|
return c.repoMgr.Update(ctx, repository, properties...)
|
|
|
|
}
|