From dcb048e0c095fde4d835e97e6a411316418ee821 Mon Sep 17 00:00:00 2001 From: "A. Diamond" Date: Tue, 21 Nov 2023 15:04:15 -0500 Subject: [PATCH] Moved remote repo client connection test into client. --- core/aptrust_client_v3.go | 27 ++++++++++++++++++++++++--- core/lockss_client_v2.go | 4 ++-- core/remote_repo_client.go | 2 +- core/remote_repository.go | 27 +++++---------------------- 4 files changed, 32 insertions(+), 28 deletions(-) diff --git a/core/aptrust_client_v3.go b/core/aptrust_client_v3.go index a1a8875..42ed883 100644 --- a/core/aptrust_client_v3.go +++ b/core/aptrust_client_v3.go @@ -1,8 +1,13 @@ package core import ( + "fmt" + "net/http" + "net/url" + "github.com/APTrust/dart-runner/constants" "github.com/APTrust/dart-runner/util" + apt_network "github.com/APTrust/preservation-services/network" ) func init() { @@ -75,9 +80,25 @@ func (client *APTrustClientV3) AvailableHTMLReports() []util.NameValuePair { // TestConnection tests a connection to the remote repo. It returns true // or false to describe whether the connection succeeded. Check the error // if the connection did not succeed. -func (client *APTrustClientV3) TestConnection() (bool, error) { - - return true, nil +func (client *APTrustClientV3) TestConnection() error { + registryClient, err := apt_network.NewRegistryClient( + client.config.Url, + client.version, + client.config.UserID, + client.config.APIToken, + Dart.Log, + ) + if err != nil { + return err + } + params := url.Values{} + params.Add("per_page", "1") + resp := registryClient.WorkItemList(params) + if resp.Response.StatusCode == http.StatusUnauthorized || resp.Response.StatusCode == http.StatusForbidden { + return fmt.Errorf("Server returned status %d. Be sure your user id and API token are correct.", resp.Response.StatusCode) + } + // Other errors should be OK here. They indicate that we did successfully authenticate. + return nil } // RunHTMLReport runs the named report and returns HTML suitable for diff --git a/core/lockss_client_v2.go b/core/lockss_client_v2.go index d7516da..15757e6 100644 --- a/core/lockss_client_v2.go +++ b/core/lockss_client_v2.go @@ -77,9 +77,9 @@ func (client *LOCKSSClientV2) AvailableHTMLReports() []util.NameValuePair { // TestConnection tests a connection to the remote repo. It returns true // or false to describe whether the connection succeeded. Check the error // if the connection did not succeed. -func (client *LOCKSSClientV2) TestConnection() (bool, error) { +func (client *LOCKSSClientV2) TestConnection() error { - return true, nil + return nil } // RunHTMLReport runs the named report and returns HTML suitable for diff --git a/core/remote_repo_client.go b/core/remote_repo_client.go index 17b4964..c48302b 100644 --- a/core/remote_repo_client.go +++ b/core/remote_repo_client.go @@ -25,7 +25,7 @@ type RemoteRepoClient interface { Description() string AvailableHTMLReports() []util.NameValuePair RunHTMLReport(string) (string, error) - TestConnection() (bool, error) + TestConnection() error } // RegisterRepoClient registers a remote repo client, so that diff --git a/core/remote_repository.go b/core/remote_repository.go index 507a9c3..04e2cde 100644 --- a/core/remote_repository.go +++ b/core/remote_repository.go @@ -2,13 +2,10 @@ package core import ( "fmt" - "net/http" - "net/url" "strings" "github.com/APTrust/dart-runner/constants" "github.com/APTrust/dart-runner/util" - apt_network "github.com/APTrust/preservation-services/network" "github.com/google/uuid" ) @@ -115,26 +112,12 @@ func (repo *RemoteRepository) IsDeletable() bool { } func (repo *RemoteRepository) TestConnection() error { - // Once we support more than a single client, - // we'll have to look up the PluginId here. For now, - // we'll just use the APTrust client, because that's - // the only one that exists. LOCKSS should be coming later. - client, err := apt_network.NewRegistryClient( - repo.Url, - "v3", - repo.UserID, - repo.APIToken, - Dart.Log, - ) + if !util.LooksLikeUUID(repo.PluginID) { + return fmt.Errorf("Please choose a client plugin before testing connection.") + } + client, err := GetRemoteRepoClient(repo) if err != nil { return err } - params := url.Values{} - params.Add("per_page", "1") - resp := client.WorkItemList(params) - if resp.Response.StatusCode == http.StatusUnauthorized || resp.Response.StatusCode == http.StatusForbidden { - return fmt.Errorf("Server returned status %d. Be sure your user id and API token are correct.", resp.Response.StatusCode) - } - // Other errors should be OK here. They indicate that we did successfully authenticate. - return nil + return client.TestConnection() }