Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Repository optimization #7

Merged
merged 2 commits into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ type Client interface {
BlobstoreRead(string) (*Blobstore, error)
BlobstoreUpdate(string, Blobstore) error
BlobstoreDelete(string) error
RepositoryCreate(Repository, string, string) error
RepositoryUpdate(string, Repository, string, string) error
RepositoryCreate(Repository) error
RepositoryRead(string) (*Repository, error)
RepositoryUpdate(string, Repository) error
RepositoryDelete(string) error
RoleCreate(Role) error
RoleRead(string) (*Role, error)
Expand Down
34 changes: 17 additions & 17 deletions repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (
const (
repositoryAPIEndpoint = "service/rest/beta/repositories"

FormatApt = "apt"
FormatBower = "bower"
FormatDocker = "docker"
FormatMaven2 = "maven2"

TypeHosted = "hosted"
TypeGroup = "group"
TypeProxy = "proxy"
RepositoryFormatApt = "apt"
RepositoryFormatBower = "bower"
RepositoryFormatDocker = "docker"
RepositoryFormatMaven2 = "maven2"

RepositoryTypeHosted = "hosted"
RepositoryTypeGroup = "group"
RepositoryTypeProxy = "proxy"
)

// Repository ...
Expand Down Expand Up @@ -91,8 +91,8 @@ type RepositoryDocker struct {

// RepositoryDockerProxy contains data of a Docker Proxy Repository
type RepositoryDockerProxy struct {
IndexType string `json:"indexType"`
IndexURL string `json:"indexUrl"`
IndexType string `json:"indexType"`
IndexURL *string `json:"indexUrl,omitempty"`
}

// RepositoryGroup contains repository group configuration data
Expand Down Expand Up @@ -146,9 +146,9 @@ type RepositoryProxy struct {

// RepositoryStorage contains repository storage
type RepositoryStorage struct {
BlobStoreName string `json:"blobStoreName"`
StrictContentTypeValidation bool `json:"strictContentTypeValidation"`
WritePolicy string `json:"writePolicy,omitempty"`
BlobStoreName string `json:"blobStoreName"`
StrictContentTypeValidation bool `json:"strictContentTypeValidation"`
WritePolicy *string `json:"writePolicy,omitempty"`
}

func jsonUnmarshalRepositories(data []byte) ([]Repository, error) {
Expand All @@ -159,13 +159,13 @@ func jsonUnmarshalRepositories(data []byte) ([]Repository, error) {
return repositories, nil
}

func (c client) RepositoryCreate(repo Repository, format string, repoType string) error {
func (c client) RepositoryCreate(repo Repository) error {
data, err := jsonMarshalInterfaceToIOReader(repo)
if err != nil {
return err
}

body, resp, err := c.Post(fmt.Sprintf("%s/%s/%s", repositoryAPIEndpoint, format, repoType), data)
body, resp, err := c.Post(fmt.Sprintf("%s/%s/%s", repositoryAPIEndpoint, repo.Format, repo.Type), data)
if err != nil {
return err
}
Expand Down Expand Up @@ -200,13 +200,13 @@ func (c client) RepositoryRead(id string) (*Repository, error) {
return nil, nil
}

func (c client) RepositoryUpdate(id string, repo Repository, format string, repoType string) error {
func (c client) RepositoryUpdate(id string, repo Repository) error {
data, err := jsonMarshalInterfaceToIOReader(repo)
if err != nil {
return err
}

body, resp, err := c.Put(fmt.Sprintf("%s/%s/%s/%s", repositoryAPIEndpoint, format, repoType, id), data)
body, resp, err := c.Put(fmt.Sprintf("%s/%s/%s/%s", repositoryAPIEndpoint, repo.Format, repo.Type, id), data)
if err != nil {
return err
}
Expand Down
102 changes: 83 additions & 19 deletions repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func TestJSONUnmarshalRepositories(t *testing.T) {
assert.Equal(t, 1, len(repositories))

repo := repositories[0]
assert.Equal(t, repo.Format, FormatMaven2)
assert.Equal(t, repo.Type, TypeProxy)
assert.Equal(t, repo.Format, RepositoryFormatMaven2)
assert.Equal(t, repo.Type, RepositoryTypeProxy)
assert.Nil(t, repo.RepositoryDocker)
assert.NotNil(t, repo.RepositoryHTTPClient)
}
Expand Down Expand Up @@ -82,54 +82,62 @@ func testJSONUnmarshalRepositories() string {
}]`)
}

func getTestRepositoryApt(name string) Repository {
func getTestRepositoryAptHosted(name string) Repository {
writePolicy := "ALLOW_ONCE"

return Repository{
Name: name,
Online: true,
Type: RepositoryTypeHosted,
Format: RepositoryFormatApt,

RepositoryApt: &RepositoryApt{
Distribution: "bionic",
},
RepositoryAptSigning: &RepositoryAptSigning{
Keypair: "string",
Passphrase: "string",
},
Name: name,
Online: true,
RepositoryCleanup: &RepositoryCleanup{
PolicyNames: []string{"weekly-cleanup"},
},
RepositoryStorage: &RepositoryStorage{
BlobStoreName: "default",
StrictContentTypeValidation: true,
WritePolicy: "allow_once",
WritePolicy: &writePolicy,
},
}
}

func TestRepositoryAptHosted(t *testing.T) {
client := NewClient(getDefaultConfig())
repo := getTestRepositoryApt("test-apt-repo-hosted")
repo := getTestRepositoryAptHosted("test-apt-repo-hosted")

err := client.RepositoryCreate(repo, FormatApt, TypeHosted)
err := client.RepositoryCreate(repo)
assert.Nil(t, err)

updatedRepo := repo
updatedRepo.Online = false

err = client.RepositoryUpdate(repo.Name, updatedRepo, FormatApt, TypeHosted)
err = client.RepositoryUpdate(repo.Name, updatedRepo)
assert.Nil(t, err)

err = client.RepositoryDelete(repo.Name)
assert.Nil(t, err)
}

func getTestRepositoryDockerWithPorts(name string) Repository {
func getTestRepositoryDockerHostedWithPorts(name string) Repository {
httpPort := new(int)
httpsPort := new(int)
*httpPort = 8082
*httpsPort = 8083
writePolicy := "ALLOW_ONCE"

return Repository{
Name: name,
Online: true,
Format: RepositoryFormatDocker,
Type: RepositoryTypeHosted,
RepositoryCleanup: &RepositoryCleanup{
PolicyNames: []string{"weekly-cleanup"},
},
Expand All @@ -142,32 +150,36 @@ func getTestRepositoryDockerWithPorts(name string) Repository {
RepositoryStorage: &RepositoryStorage{
BlobStoreName: "default",
StrictContentTypeValidation: true,
WritePolicy: "ALLOW_ONCE",
WritePolicy: &writePolicy,
},
}
}

func TestRepositoryDockerHostedWithPorts(t *testing.T) {
client := NewClient(getDefaultConfig())
repo := getTestRepositoryDockerWithPorts("test-docker-repo-hosted-with-ports")
repo := getTestRepositoryDockerHostedWithPorts("test-docker-repo-hosted-with-ports")

err := client.RepositoryCreate(repo, FormatDocker, TypeHosted)
err := client.RepositoryCreate(repo)
assert.Nil(t, err)

updatedRepo := repo
updatedRepo.Online = false

err = client.RepositoryUpdate(repo.Name, updatedRepo, FormatBower, TypeHosted)
err = client.RepositoryUpdate(repo.Name, updatedRepo)
assert.Nil(t, err)

err = client.RepositoryDelete(repo.Name)
assert.Nil(t, err)
}

func getTestRepositoryDockerWithoutPorts(name string) Repository {
func getTestRepositoryDockerHostedWithoutPorts(name string) Repository {
writePolicy := "ALLOW_ONCE"

return Repository{
Name: name,
Online: true,
Format: RepositoryFormatDocker,
Type: RepositoryTypeHosted,
RepositoryCleanup: &RepositoryCleanup{
PolicyNames: []string{"weekly-cleanup"},
},
Expand All @@ -178,22 +190,22 @@ func getTestRepositoryDockerWithoutPorts(name string) Repository {
RepositoryStorage: &RepositoryStorage{
BlobStoreName: "default",
StrictContentTypeValidation: true,
WritePolicy: "ALLOW_ONCE",
WritePolicy: &writePolicy,
},
}
}

func TestRepositoryDockerHostedWithoutPorts(t *testing.T) {
client := NewClient(getDefaultConfig())
repo := getTestRepositoryDockerWithoutPorts("test-docker-repo-hosted-with-ports")
repo := getTestRepositoryDockerHostedWithoutPorts("test-docker-repo-hosted-with-ports")

err := client.RepositoryCreate(repo, FormatDocker, TypeHosted)
err := client.RepositoryCreate(repo)
assert.Nil(t, err)

updatedRepo := repo
updatedRepo.Online = false

err = client.RepositoryUpdate(repo.Name, updatedRepo, FormatDocker, TypeHosted)
err = client.RepositoryUpdate(repo.Name, updatedRepo)
assert.Nil(t, err)

err = client.RepositoryDelete(repo.Name)
Expand All @@ -211,3 +223,55 @@ func TestRepositoryMavenRead(t *testing.T) {
assert.NotNil(t, repo.RepositoryGroup)
assert.Greater(t, len(repo.RepositoryGroup.MemberNames), 0)
}

func TestRepositoryDockerProxy(t *testing.T) {
client := NewClient(getDefaultConfig())
repo := getTestRepositoryDockerProxy("test-docker-repo-proxy")

err := client.RepositoryCreate(repo)
assert.Nil(t, err)

createdRepo, err := client.RepositoryRead(repo.Name)
assert.Nil(t, err)
assert.NotNil(t, createdRepo)

if createdRepo != nil {
assert.Equal(t, repo.Name, createdRepo.Name)
assert.Equal(t, repo.Type, createdRepo.Type)
assert.Equal(t, repo.Format, createdRepo.Format)

err := client.RepositoryDelete(repo.Name)
assert.Nil(t, err)
}
}

func getTestRepositoryDockerProxy(name string) Repository {
return Repository{
Name: name,
Online: true,
Type: RepositoryTypeProxy,
Format: RepositoryFormatDocker,
RepositoryCleanup: &RepositoryCleanup{
PolicyNames: []string{"weekly-cleanup"},
},
RepositoryDocker: &RepositoryDocker{
V1Enabled: false,
ForceBasicAuth: true,
},
RepositoryDockerProxy: &RepositoryDockerProxy{
IndexType: "HUB",
},
RepositoryHTTPClient: &RepositoryHTTPClient{
Authentication: RepositoryHTTPClientAuthentication{
Type: "username",
},
},
RepositoryNegativeCache: &RepositoryNegativeCache{},
RepositoryProxy: &RepositoryProxy{
RemoteURL: "https://registry-1.docker.io",
},
RepositoryStorage: &RepositoryStorage{
BlobStoreName: "default",
},
}
}