Skip to content

Commit

Permalink
Merge pull request #22 from mittwald/feature/getByName
Browse files Browse the repository at this point in the history
make it possible to retrieve projects, registries and replications by its name
  • Loading branch information
elenz97 authored May 26, 2020
2 parents 5b643c1 + 3bf26c2 commit f0564c6
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
18 changes: 18 additions & 0 deletions project.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package harbor

import (
"errors"
"fmt"
"net/url"

Expand Down Expand Up @@ -34,6 +35,23 @@ func (s *ProjectClient) CheckProject(projectName string) error {
return CheckResponse(errs, resp, 200)
}

// GetProjectByName
// Get a project by its name
func (s *ProjectClient) GetProjectByName(name string) (Project, error) {
projects, err := s.ListProjects(ListProjectsOptions{Name: name})
if err != nil {
return Project{}, err
}

for i, pr := range projects {
if pr.Name == name {
return projects[i], nil
}
}

return Project{}, errors.New("project not found")
}

// GetProjectByID
// Return specific project details
func (s *ProjectClient) GetProjectByID(pid int64) (Project, error) {
Expand Down
18 changes: 18 additions & 0 deletions registry.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package harbor

import (
"errors"
"fmt"

"github.com/parnurzeal/gorequest"
Expand Down Expand Up @@ -30,6 +31,23 @@ func (s *RegistryClient) ListRegistries(name string) ([]Registry, error) {
return r, CheckResponse(errs, resp, 200)
}

// GetRegistryByName
// Get a registry by its name
func (s *RegistryClient) GetRegistryByName(name string) (Registry, error) {
registries, err := s.ListRegistries(name)
if err != nil {
return Registry{}, err
}

for i, reg := range registries {
if reg.Name == name {
return registries[i], nil
}
}

return Registry{}, errors.New("registry not found")
}

// GetRegistryByID
// Get a registry by ID
func (s *RegistryClient) GetRegistryByID(id int64) (Registry, error) {
Expand Down
19 changes: 19 additions & 0 deletions replication.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package harbor

import (
"errors"

"github.com/parnurzeal/gorequest"
)

Expand Down Expand Up @@ -28,6 +30,23 @@ func (s *ReplicationClient) ListReplicationPolicies(name string) ([]ReplicationP
return rp, CheckResponse(errs, resp, 200)
}

// GetReplicationPolicyByName
// Get a replication policy by its name
func (s *ReplicationClient) GetReplicationPolicyByName(name string) (ReplicationPolicy, error) {
replications, err := s.ListReplicationPolicies(name)
if err != nil {
return ReplicationPolicy{}, err
}

for i, repl := range replications {
if repl.Name == name {
return replications[i], nil
}
}

return ReplicationPolicy{}, errors.New("replication policy not found")
}

// GetReplicationPolicies
// Get a replication policy by ID
func (s *ReplicationClient) GetReplicationPolicyByID(id int64) (ReplicationPolicy, error) {
Expand Down

0 comments on commit f0564c6

Please sign in to comment.