Implement Convert interface

This commit is contained in:
Wenkai Yin 2017-11-03 14:21:32 +08:00
parent 08dff99008
commit f86a4be84d
5 changed files with 279 additions and 3 deletions

View File

@ -0,0 +1,59 @@
// Copyright (c) 2017 VMware, Inc. All Rights Reserved.
//
// 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 source
import (
"github.com/vmware/harbor/src/common/utils/log"
"github.com/vmware/harbor/src/replication"
"github.com/vmware/harbor/src/replication/models"
"github.com/vmware/harbor/src/replication/registry"
)
// RepositoryConvertor implement Convertor interface, convert projects to repositories
type RepositoryConvertor struct {
registry registry.Adaptor
}
// NewRepositoryConvertor returns an instance of RepositoryConvertor
func NewRepositoryConvertor(registry registry.Adaptor) *RepositoryConvertor {
return &RepositoryConvertor{
registry: registry,
}
}
// Convert projects to repositories
func (r *RepositoryConvertor) Convert(items []models.FilterItem) []models.FilterItem {
result := []models.FilterItem{}
for _, item := range items {
if item.Kind != replication.FilterItemKindProject {
log.Warningf("unexpected filter item kind for repository convertor, expected %s got %s, skip",
replication.FilterItemKindProject, item.Kind)
continue
}
repositories := r.registry.GetRepositories(item.Value)
for _, repository := range repositories {
result = append(result, models.FilterItem{
Kind: replication.FilterItemKindRepository,
Value: repository.Name,
// public is used to create project if it does not exist when replicating
Metadata: map[string]interface{}{
"public": item.Metadata["public"],
},
})
}
}
return result
}

View File

@ -0,0 +1,101 @@
// Copyright (c) 2017 VMware, Inc. All Rights Reserved.
//
// 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 source
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/vmware/harbor/src/replication"
"github.com/vmware/harbor/src/replication/models"
)
func TestRepositoryConvert(t *testing.T) {
items := []models.FilterItem{
models.FilterItem{
Kind: replication.FilterItemKindProject,
Value: "library",
Metadata: map[string]interface{}{
"public": true,
},
},
models.FilterItem{
Kind: replication.FilterItemKindRepository,
},
}
expected := []models.FilterItem{
models.FilterItem{
Kind: replication.FilterItemKindRepository,
Value: "library/ubuntu",
Metadata: map[string]interface{}{
"public": true,
},
},
models.FilterItem{
Kind: replication.FilterItemKindRepository,
Value: "library/centos",
Metadata: map[string]interface{}{
"public": true,
},
},
}
convertor := NewRepositoryConvertor(&fakeRegistryAdaptor{})
assert.EqualValues(t, expected, convertor.Convert(items))
}
type fakeRegistryAdaptor struct{}
func (f *fakeRegistryAdaptor) Kind() string {
return "fake"
}
func (f *fakeRegistryAdaptor) GetNamespaces() []models.Namespace {
return nil
}
func (f *fakeRegistryAdaptor) GetNamespace(name string) models.Namespace {
return models.Namespace{}
}
func (f *fakeRegistryAdaptor) GetRepositories(namespace string) []models.Repository {
return []models.Repository{
models.Repository{
Name: "library/ubuntu",
},
models.Repository{
Name: "library/centos",
},
}
}
func (f *fakeRegistryAdaptor) GetRepository(name string, namespace string) models.Repository {
return models.Repository{}
}
func (f *fakeRegistryAdaptor) GetTags(repositoryName string, namespace string) []models.Tag {
return []models.Tag{
models.Tag{
Name: "library/ubuntu:14.04",
},
models.Tag{
Name: "library/ubuntu:16.04",
},
}
}
func (f *fakeRegistryAdaptor) GetTag(name string, repositoryName string, namespace string) models.Tag {
return models.Tag{}
}

View File

@ -24,15 +24,15 @@ import (
var tcfilter = NewTagCombinationFilter()
func TestTagCombinationFilteInit(t *testing.T) {
func TestTagCombinationFilterInit(t *testing.T) {
assert.Nil(t, tcfilter.Init())
}
func TestTagCombinationFilteGetConvertor(t *testing.T) {
func TestTagCombinationFilterGetConvertor(t *testing.T) {
assert.Nil(t, tcfilter.GetConvertor())
}
func TestTagCombinationFilteDoFilter(t *testing.T) {
func TestTagCombinationFilterDoFilter(t *testing.T) {
items := []models.FilterItem{
models.FilterItem{
Kind: replication.FilterItemKindProject,

View File

@ -0,0 +1,59 @@
// Copyright (c) 2017 VMware, Inc. All Rights Reserved.
//
// 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 source
import (
"github.com/vmware/harbor/src/common/utils/log"
"github.com/vmware/harbor/src/replication"
"github.com/vmware/harbor/src/replication/models"
"github.com/vmware/harbor/src/replication/registry"
)
// TagConvertor implement Convertor interface, convert repositories to tags
type TagConvertor struct {
registry registry.Adaptor
}
// NewTagConvertor returns an instance of TagConvertor
func NewTagConvertor(registry registry.Adaptor) *TagConvertor {
return &TagConvertor{
registry: registry,
}
}
//Convert repositories to tags
func (t *TagConvertor) Convert(items []models.FilterItem) []models.FilterItem {
result := []models.FilterItem{}
for _, item := range items {
if item.Kind != replication.FilterItemKindRepository {
log.Warningf("unexpected filter item kind for tag convertor, expected %s got %s, skip",
replication.FilterItemKindRepository, item.Kind)
continue
}
tags := t.registry.GetTags(item.Value, "")
for _, tag := range tags {
result = append(result, models.FilterItem{
Kind: replication.FilterItemKindTag,
Value: tag.Name,
// public is used to create project if it does not exist when replicating
Metadata: map[string]interface{}{
"public": item.Metadata["public"],
},
})
}
}
return result
}

View File

@ -0,0 +1,57 @@
// Copyright (c) 2017 VMware, Inc. All Rights Reserved.
//
// 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 source
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/vmware/harbor/src/replication"
"github.com/vmware/harbor/src/replication/models"
)
func TestTagConvert(t *testing.T) {
items := []models.FilterItem{
models.FilterItem{
Kind: replication.FilterItemKindRepository,
Value: "library/ubuntu",
Metadata: map[string]interface{}{
"public": true,
},
},
models.FilterItem{
Kind: replication.FilterItemKindProject,
},
}
expected := []models.FilterItem{
models.FilterItem{
Kind: replication.FilterItemKindTag,
Value: "library/ubuntu:14.04",
Metadata: map[string]interface{}{
"public": true,
},
},
models.FilterItem{
Kind: replication.FilterItemKindTag,
Value: "library/ubuntu:16.04",
Metadata: map[string]interface{}{
"public": true,
},
},
}
convertor := NewTagConvertor(&fakeRegistryAdaptor{})
assert.EqualValues(t, expected, convertor.Convert(items))
}