Skip to content

Commit

Permalink
add ping client (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
elenz97 authored Jan 2, 2023
1 parent bd478c4 commit f56e06d
Show file tree
Hide file tree
Showing 15 changed files with 135 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions apiv2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package apiv2

import (
"context"
"github.com/mittwald/goharbor-client/v5/apiv2/pkg/clients/ping"
"net/url"
"strings"

Expand Down Expand Up @@ -48,6 +49,7 @@ type Client interface {
health.Client
label.Client
member.Client
ping.Client
project.Client
projectmeta.Client
quota.Client
Expand All @@ -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
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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)
}
3 changes: 2 additions & 1 deletion apiv2/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions apiv2/pkg/clients/artifact/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion apiv2/pkg/clients/auditlog/auditlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package auditlog

import (
"context"

"github.com/go-openapi/runtime"

v2client "github.com/mittwald/goharbor-client/v5/apiv2/internal/api/client"
Expand Down Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions apiv2/pkg/clients/gc/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gc

import (
"context"

"github.com/go-openapi/runtime"

v2client "github.com/mittwald/goharbor-client/v5/apiv2/internal/api/client"
Expand Down
1 change: 1 addition & 0 deletions apiv2/pkg/clients/label/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions apiv2/pkg/clients/member/member.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package member

import (
"context"

"github.com/go-openapi/runtime"

v2client "github.com/mittwald/goharbor-client/v5/apiv2/internal/api/client"
Expand Down
48 changes: 48 additions & 0 deletions apiv2/pkg/clients/ping/ping.go
Original file line number Diff line number Diff line change
@@ -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
}
20 changes: 20 additions & 0 deletions apiv2/pkg/clients/ping/ping_integration_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
44 changes: 44 additions & 0 deletions apiv2/pkg/clients/ping/ping_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions apiv2/pkg/clients/projectmeta/projectmeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package projectmeta

import (
"context"

"github.com/go-openapi/runtime"

v2client "github.com/mittwald/goharbor-client/v5/apiv2/internal/api/client"
Expand Down
1 change: 1 addition & 0 deletions apiv2/pkg/clients/robot/robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package robot

import (
"context"

"github.com/go-openapi/runtime"

v2client "github.com/mittwald/goharbor-client/v5/apiv2/internal/api/client"
Expand Down
1 change: 1 addition & 0 deletions apiv2/pkg/clients/robotv1/robotv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit f56e06d

Please sign in to comment.