From 3bf26c20a5e127f99851721ddd88e85e58c7cf15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jerome=20K=C3=BCttner?= Date: Tue, 26 May 2020 08:52:07 +0200 Subject: [PATCH] add GetByName methods --- project.go | 18 ++++++++++++++++++ registry.go | 18 ++++++++++++++++++ replication.go | 19 +++++++++++++++++++ 3 files changed, 55 insertions(+) diff --git a/project.go b/project.go index f8ab4015..50f896f5 100644 --- a/project.go +++ b/project.go @@ -1,6 +1,7 @@ package harbor import ( + "errors" "fmt" "net/url" @@ -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) { diff --git a/registry.go b/registry.go index ff562933..dcf1de23 100644 --- a/registry.go +++ b/registry.go @@ -1,6 +1,7 @@ package harbor import ( + "errors" "fmt" "github.com/parnurzeal/gorequest" @@ -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) { diff --git a/replication.go b/replication.go index afaf959f..c8aeeea3 100644 --- a/replication.go +++ b/replication.go @@ -1,6 +1,8 @@ package harbor import ( + "errors" + "github.com/parnurzeal/gorequest" ) @@ -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) {