diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index ceb9db62..6baff24b 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -100,7 +100,7 @@ For example, let's look at the existing `user` package of the `v2` client: > To maintain integrity with the rest of the repository, new sub-client's `.go`-files should be prefixed with their package name. -`user.go` holds the methods that act operations on `user` objects on the `goharbor` API. The below examples are not guaranteed to be up to date. +`user.go` holds the methods that act operations on `user` objects on the `goharbor` API. The below examples are not guaranteed to be up-to-date. It contains a `RESTClient` struct that groups together the `v2` Goharbor client, client `Options` as well as a field `AuthInfo` for [openAPI's `runtime.ClientAuthInfoWriter`](https://pkg.go.dev/github.com/go-openapi/runtime#ClientAuthInfoWriter). The latter is used to authenticate requests to the `goharbor` API: diff --git a/Makefile b/Makefile index a5ff2991..0350f807 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ V1_VERSION = v1.10.15 V2_VERSION = v2.7.0 MOCKERY_VERSION = v2.14.0 GOSWAGGER_VERSION = v0.25.0 -GOLANGCI_LINT_VERSION = v1.47.2 +GOLANGCI_LINT_VERSION = v1.50.1 # Run all code generation targets generate: swagger-generate mock-generate diff --git a/apiv2/client.go b/apiv2/client.go index 6508c4a2..182ac154 100644 --- a/apiv2/client.go +++ b/apiv2/client.go @@ -2,6 +2,7 @@ package apiv2 import ( "context" + "github.com/mittwald/goharbor-client/v5/apiv2/pkg/clients/ping" "net/url" "strings" @@ -48,6 +49,7 @@ type Client interface { health.Client label.Client member.Client + ping.Client project.Client projectmeta.Client quota.Client @@ -70,6 +72,7 @@ type RESTClient struct { health *health.RESTClient label *label.RESTClient member *member.RESTClient + ping *ping.RESTClient project *project.RESTClient projectmeta *projectmeta.RESTClient quota *quota.RESTClient @@ -97,6 +100,7 @@ func NewRESTClient(v2Client *v2client.Harbor, opts *config.Options, authInfo run health: health.NewClient(v2Client, opts, authInfo), label: label.NewClient(v2Client, opts, authInfo), member: member.NewClient(v2Client, opts, authInfo), + ping: ping.NewClient(v2Client, opts, authInfo), project: project.NewClient(v2Client, opts, authInfo), projectmeta: projectmeta.NewClient(v2Client, opts, authInfo), quota: quota.NewClient(v2Client, opts, authInfo), @@ -572,3 +576,9 @@ func (c *RESTClient) UpdateProjectWebhookPolicy(ctx context.Context, projectID i func (c *RESTClient) DeleteProjectWebhookPolicy(ctx context.Context, projectID int, policyID int64) error { return c.webhook.DeleteProjectWebhookPolicy(ctx, projectID, policyID) } + +// Ping Client + +func (c *RESTClient) GetPing(ctx context.Context) (string, error) { + return c.ping.GetPing(ctx) +} diff --git a/apiv2/client_test.go b/apiv2/client_test.go index f593ebbe..cca94af9 100644 --- a/apiv2/client_test.go +++ b/apiv2/client_test.go @@ -4,9 +4,10 @@ package apiv2 import ( "context" - "github.com/go-openapi/runtime" "net/url" + "github.com/go-openapi/runtime" + "github.com/mittwald/goharbor-client/v5/apiv2/model" runtimeclient "github.com/go-openapi/runtime/client" diff --git a/apiv2/pkg/clients/artifact/artifact.go b/apiv2/pkg/clients/artifact/artifact.go index e676bbf8..e9d159f8 100644 --- a/apiv2/pkg/clients/artifact/artifact.go +++ b/apiv2/pkg/clients/artifact/artifact.go @@ -3,6 +3,7 @@ package artifact import ( "context" "fmt" + "github.com/go-openapi/runtime" v2client "github.com/mittwald/goharbor-client/v5/apiv2/internal/api/client" "github.com/mittwald/goharbor-client/v5/apiv2/internal/api/client/artifact" diff --git a/apiv2/pkg/clients/auditlog/auditlog.go b/apiv2/pkg/clients/auditlog/auditlog.go index c1844b1e..c60c41d8 100644 --- a/apiv2/pkg/clients/auditlog/auditlog.go +++ b/apiv2/pkg/clients/auditlog/auditlog.go @@ -2,6 +2,7 @@ package auditlog import ( "context" + "github.com/go-openapi/runtime" v2client "github.com/mittwald/goharbor-client/v5/apiv2/internal/api/client" @@ -54,7 +55,7 @@ func (c *RESTClient) ListAuditLogs(ctx context.Context) ([]*model.AuditLog, erro if err != nil { return nil, handleSwaggerAuditLogErrors(err) } - + if len(resp.Payload) == 0 { break } diff --git a/apiv2/pkg/clients/gc/gc.go b/apiv2/pkg/clients/gc/gc.go index 61967fa2..cc3ffd35 100644 --- a/apiv2/pkg/clients/gc/gc.go +++ b/apiv2/pkg/clients/gc/gc.go @@ -2,6 +2,7 @@ package gc import ( "context" + "github.com/go-openapi/runtime" v2client "github.com/mittwald/goharbor-client/v5/apiv2/internal/api/client" diff --git a/apiv2/pkg/clients/label/label.go b/apiv2/pkg/clients/label/label.go index f40429e0..1b403732 100644 --- a/apiv2/pkg/clients/label/label.go +++ b/apiv2/pkg/clients/label/label.go @@ -3,6 +3,7 @@ package label import ( "context" "fmt" + "github.com/go-openapi/runtime" v2client "github.com/mittwald/goharbor-client/v5/apiv2/internal/api/client" "github.com/mittwald/goharbor-client/v5/apiv2/internal/api/client/label" diff --git a/apiv2/pkg/clients/member/member.go b/apiv2/pkg/clients/member/member.go index 3ce238aa..87afdb40 100644 --- a/apiv2/pkg/clients/member/member.go +++ b/apiv2/pkg/clients/member/member.go @@ -2,6 +2,7 @@ package member import ( "context" + "github.com/go-openapi/runtime" v2client "github.com/mittwald/goharbor-client/v5/apiv2/internal/api/client" diff --git a/apiv2/pkg/clients/ping/ping.go b/apiv2/pkg/clients/ping/ping.go new file mode 100644 index 00000000..c7cee0c3 --- /dev/null +++ b/apiv2/pkg/clients/ping/ping.go @@ -0,0 +1,48 @@ +package ping + +import ( + "context" + + "github.com/go-openapi/runtime" + v2client "github.com/mittwald/goharbor-client/v5/apiv2/internal/api/client" + "github.com/mittwald/goharbor-client/v5/apiv2/internal/api/client/ping" + "github.com/mittwald/goharbor-client/v5/apiv2/pkg/config" +) + +// RESTClient is a subclient for handling ping related actions. +type RESTClient struct { + // Options contains optional configuration when making API calls. + Options *config.Options + + // The new client of the harbor v2 API + V2Client *v2client.Harbor + + // AuthInfo contains the auth information that is provided on API calls. + AuthInfo runtime.ClientAuthInfoWriter +} + +func NewClient(v2Client *v2client.Harbor, opts *config.Options, authInfo runtime.ClientAuthInfoWriter) *RESTClient { + return &RESTClient{ + Options: opts, + V2Client: v2Client, + AuthInfo: authInfo, + } +} + +type Client interface { + GetPing(ctx context.Context) (string, error) +} + +func (c *RESTClient) GetPing(ctx context.Context) (string, error) { + params := &ping.GetPingParams{ + Context: ctx, + } + params.WithTimeout(c.Options.Timeout) + + resp, err := c.V2Client.Ping.GetPing(params, c.AuthInfo) + if err != nil { + return "", err + } + + return resp.Payload, nil +} diff --git a/apiv2/pkg/clients/ping/ping_integration_test.go b/apiv2/pkg/clients/ping/ping_integration_test.go new file mode 100644 index 00000000..285e5ae2 --- /dev/null +++ b/apiv2/pkg/clients/ping/ping_integration_test.go @@ -0,0 +1,20 @@ +//go:build integration + +package ping + +import ( + "context" + "testing" + + clienttesting "github.com/mittwald/goharbor-client/v5/apiv2/pkg/testing" + "github.com/stretchr/testify/require" +) + +func TestAPIGetPing(t *testing.T) { + ctx := context.Background() + c := NewClient(clienttesting.V2SwaggerClient, clienttesting.DefaultOpts, clienttesting.AuthInfo) + + res, err := c.GetPing(ctx) + require.NoError(t, err) + require.Equal(t, "Pong", res) +} diff --git a/apiv2/pkg/clients/ping/ping_test.go b/apiv2/pkg/clients/ping/ping_test.go new file mode 100644 index 00000000..705e1446 --- /dev/null +++ b/apiv2/pkg/clients/ping/ping_test.go @@ -0,0 +1,44 @@ +//go:build !integration + +package ping + +import ( + "context" + "testing" + + "github.com/mittwald/goharbor-client/v5/apiv2/internal/api/client/ping" + "github.com/mittwald/goharbor-client/v5/apiv2/mocks" + clienttesting "github.com/mittwald/goharbor-client/v5/apiv2/pkg/testing" + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" +) + +var ctx = context.Background() + +func APIandMockClientsForTests() (*RESTClient, *clienttesting.MockClients) { + desiredMockClients := &clienttesting.MockClients{ + Project: mocks.MockProjectClientService{}, + } + + v2Client := clienttesting.BuildV2ClientWithMocks(desiredMockClients) + + cl := NewClient(v2Client, clienttesting.DefaultOpts, clienttesting.AuthInfo) + + return cl, desiredMockClients +} +func TestRESTClient_GetPing(t *testing.T) { + apiClient, mockClient := APIandMockClientsForTests() + + getParams := &ping.GetPingParams{ + Context: ctx, + } + + getParams.WithTimeout(apiClient.Options.Timeout) + + mockClient.Ping.On("GetPing", getParams, mock.AnythingOfType("runtime.ClientAuthInfoWriterFunc")).Return(&ping.GetPingOK{}, nil) + + _, err := apiClient.GetPing(ctx) + require.NoError(t, err) + + mockClient.Ping.AssertExpectations(t) +} diff --git a/apiv2/pkg/clients/projectmeta/projectmeta.go b/apiv2/pkg/clients/projectmeta/projectmeta.go index b2cd129c..682f5b8b 100644 --- a/apiv2/pkg/clients/projectmeta/projectmeta.go +++ b/apiv2/pkg/clients/projectmeta/projectmeta.go @@ -2,6 +2,7 @@ package projectmeta import ( "context" + "github.com/go-openapi/runtime" v2client "github.com/mittwald/goharbor-client/v5/apiv2/internal/api/client" diff --git a/apiv2/pkg/clients/robot/robot.go b/apiv2/pkg/clients/robot/robot.go index 49545012..92fa8b40 100644 --- a/apiv2/pkg/clients/robot/robot.go +++ b/apiv2/pkg/clients/robot/robot.go @@ -2,6 +2,7 @@ package robot import ( "context" + "github.com/go-openapi/runtime" v2client "github.com/mittwald/goharbor-client/v5/apiv2/internal/api/client" diff --git a/apiv2/pkg/clients/robotv1/robotv1.go b/apiv2/pkg/clients/robotv1/robotv1.go index a90a5dfa..176b07e6 100644 --- a/apiv2/pkg/clients/robotv1/robotv1.go +++ b/apiv2/pkg/clients/robotv1/robotv1.go @@ -2,6 +2,7 @@ package robotv1 import ( "context" + "github.com/go-openapi/runtime" v2client "github.com/mittwald/goharbor-client/v5/apiv2/internal/api/client" "github.com/mittwald/goharbor-client/v5/apiv2/internal/api/client/robotv1"