Skip to content

Commit

Permalink
Merge pull request #180 from sergimrz/add/scanAll-client
Browse files Browse the repository at this point in the history
add scanall client
  • Loading branch information
elenz97 authored Nov 14, 2023
2 parents e174a12 + b1d1947 commit 69162b1
Show file tree
Hide file tree
Showing 7 changed files with 377 additions and 4 deletions.
16 changes: 16 additions & 0 deletions apiv2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/mittwald/goharbor-client/v5/apiv2/pkg/clients/configure"
"github.com/mittwald/goharbor-client/v5/apiv2/pkg/clients/ping"
"github.com/mittwald/goharbor-client/v5/apiv2/pkg/clients/scanall"
"github.com/mittwald/goharbor-client/v5/apiv2/pkg/clients/statistic"

"github.com/mittwald/goharbor-client/v5/apiv2/pkg/clients/purge"
Expand Down Expand Up @@ -69,6 +70,7 @@ type Client interface {
retention.Client
robot.Client
robotv1.Client
scanall.Client
systeminfo.Client
user.Client
webhook.Client
Expand All @@ -94,6 +96,7 @@ type RESTClient struct {
retention *retention.RESTClient
robot *robot.RESTClient
robotv1 *robotv1.RESTClient
scanall *scanall.RESTClient
statistic *statistic.RESTClient
systeminfo *systeminfo.RESTClient
user *user.RESTClient
Expand Down Expand Up @@ -125,6 +128,7 @@ func NewRESTClient(v2Client *v2client.Harbor, opts *config.Options, authInfo run
retention: retention.NewClient(v2Client, opts, authInfo),
robot: robot.NewClient(v2Client, opts, authInfo),
robotv1: robotv1.NewClient(v2Client, opts, authInfo),
scanall: scanall.NewClient(v2Client, opts, authInfo),
statistic: statistic.NewClient(v2Client, opts, authInfo),
systeminfo: systeminfo.NewClient(v2Client, opts, authInfo),
user: user.NewClient(v2Client, opts, authInfo),
Expand Down Expand Up @@ -562,6 +566,18 @@ func (c *RESTClient) GetStatistic(ctx context.Context) (*modelv2.Statistic, erro
return c.statistic.GetStatistic(ctx)
}

// Scanall Client

func (c *RESTClient) CreateScanAllSchedule(ctx context.Context, schedule *modelv2.Schedule) error {
return c.scanall.CreateScanAllSchedule(ctx, schedule)
}
func (c *RESTClient) GetScanAllSchedule(ctx context.Context) (*modelv2.Schedule, error) {
return c.scanall.GetScanAllSchedule(ctx)
}
func (c *RESTClient) UpdateScanAllSchedule(ctx context.Context, schedule *modelv2.Schedule) error {
return c.scanall.UpdateScanAllSchedule(ctx, schedule)
}

// Systeminfo Client

func (c *RESTClient) GetSystemInfo(ctx context.Context) (*modelv2.GeneralInfo, error) {
Expand Down
89 changes: 89 additions & 0 deletions apiv2/pkg/clients/scanall/scanall.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package scanall

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/scan_all"
"github.com/mittwald/goharbor-client/v5/apiv2/model"
"github.com/mittwald/goharbor-client/v5/apiv2/pkg/config"
"github.com/mittwald/goharbor-client/v5/apiv2/pkg/errors"
)

const ()

// RESTClient is a subclient for handling project 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 {
CreateScanAllSchedule(ctx context.Context, schedule *model.Schedule) error
GetScanAllSchedule(ctx context.Context) (*model.Schedule, error)
UpdateScanAllSchedule(ctx context.Context, schedule *model.Schedule) error
}

// CreateScanAllSchedule creates a new scan all schedule.
func (c *RESTClient) CreateScanAllSchedule(ctx context.Context, schedule *model.Schedule) error {
params := &scan_all.CreateScanAllScheduleParams{
Context: ctx,
Schedule: schedule,
}

params.WithTimeout(c.Options.Timeout)

_, err := c.V2Client.ScanAll.CreateScanAllSchedule(params, c.AuthInfo)

return handleSwaggerScanallErrors(err)

}

// GetScanAllSchedule returns the scan all schedule.
func (c *RESTClient) GetScanAllSchedule(ctx context.Context) (*model.Schedule, error) {
params := &scan_all.GetScanAllScheduleParams{
Context: ctx,
}

params.WithTimeout(c.Options.Timeout)

resp, err := c.V2Client.ScanAll.GetScanAllSchedule(params, c.AuthInfo)
if err != nil {
return nil, err
}
if resp.Payload == nil {
return nil, &errors.ErrNotFound{}
}
return resp.Payload, handleSwaggerScanallErrors(err)
}

// CreateScanAllSchedule creates a new scan all schedule.
func (c *RESTClient) UpdateScanAllSchedule(ctx context.Context, schedule *model.Schedule) error {
params := &scan_all.UpdateScanAllScheduleParams{
Context: ctx,
Schedule: schedule,
}

params.WithTimeout(c.Options.Timeout)

_, err := c.V2Client.ScanAll.UpdateScanAllSchedule(params, c.AuthInfo)

return handleSwaggerScanallErrors(err)

}
29 changes: 29 additions & 0 deletions apiv2/pkg/clients/scanall/scanall_errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package scanall

import (
"net/http"

"github.com/go-openapi/runtime"

"github.com/mittwald/goharbor-client/v5/apiv2/pkg/errors"
)

// handleRegistryErrors takes a swagger generated error as input,
// which usually does not contain any form of error message,
// and outputs a new error with a proper message.
func handleSwaggerScanallErrors(in error) error {
t, ok := in.(*runtime.APIError)
if ok {
switch t.Code {
case http.StatusBadRequest:
return &errors.ErrBadRequest{}
case http.StatusUnauthorized:
return &errors.ErrUnauthorized{}
case http.StatusForbidden:
return &errors.ErrForbidden{}
case http.StatusInternalServerError:
return &errors.ErrInternalErrors{}
}
}
return in
}
116 changes: 116 additions & 0 deletions apiv2/pkg/clients/scanall/scanall_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
//go:build integration

package scanall

import (
"context"
"testing"

"github.com/mittwald/goharbor-client/v5/apiv2/model"
clienttesting "github.com/mittwald/goharbor-client/v5/apiv2/pkg/testing"
"github.com/stretchr/testify/require"
)

func TestAPIScanAllCreateScanAllSchedule(t *testing.T) {
ctx := context.Background()
c := NewClient(clienttesting.V2SwaggerClient, clienttesting.DefaultOpts, clienttesting.AuthInfo)

schedule, err := c.GetScanAllSchedule(ctx)
require.NoError(t, err)
if schedule.Schedule != nil {
err = c.UpdateScanAllSchedule(ctx, &model.Schedule{
Schedule: &model.ScheduleObj{
Type: "None",
Cron: "",
},
})
require.NoError(t, err)
}
err = c.CreateScanAllSchedule(ctx, &model.Schedule{
Schedule: &model.ScheduleObj{
Type: "Daily",
Cron: "0 0 0 * * *",
},
Parameters: nil,
})

defer c.UpdateScanAllSchedule(ctx, &model.Schedule{
Schedule: &model.ScheduleObj{
Type: "None",
Cron: "",
},
})

require.NoError(t, err)
}

func TestAPIScanAllGetScanAllSchedule(t *testing.T) {
ctx := context.Background()
c := NewClient(clienttesting.V2SwaggerClient, clienttesting.DefaultOpts, clienttesting.AuthInfo)

schedule, err := c.GetScanAllSchedule(ctx)
require.NoError(t, err)

if schedule.Schedule == nil {
err = c.CreateScanAllSchedule(ctx, &model.Schedule{
Schedule: &model.ScheduleObj{
Type: "Daily",
Cron: "0 0 0 * * *",
},
Parameters: nil,
})
require.NoError(t, err)
}

schedule, err = c.GetScanAllSchedule(ctx)

defer c.UpdateScanAllSchedule(ctx, &model.Schedule{
Schedule: &model.ScheduleObj{
Type: "None",
Cron: "",
},
})

require.NoError(t, err)
require.NotEmpty(t, schedule)
}

func TestAPIScanAllUpdateScanAllSchedule(t *testing.T) {
ctx := context.Background()
c := NewClient(clienttesting.V2SwaggerClient, clienttesting.DefaultOpts, clienttesting.AuthInfo)

schedule, err := c.GetScanAllSchedule(ctx)
require.NoError(t, err)

if schedule.Schedule == nil {
err = c.CreateScanAllSchedule(ctx, &model.Schedule{
Schedule: &model.ScheduleObj{
Type: "Daily",
Cron: "0 0 0 * * *",
},
Parameters: nil,
})
require.NoError(t, err)
}

err = c.UpdateScanAllSchedule(ctx, &model.Schedule{
Schedule: &model.ScheduleObj{
Type: "Weekly",
Cron: "0 0 0 * * 0",
},
Parameters: nil,
})
require.NoError(t, err)

schedule, err = c.GetScanAllSchedule(ctx)

defer c.UpdateScanAllSchedule(ctx, &model.Schedule{
Schedule: &model.ScheduleObj{
Type: "None",
Cron: "",
},
})

require.NoError(t, err)
require.NotEmpty(t, schedule)
}
110 changes: 110 additions & 0 deletions apiv2/pkg/clients/scanall/scanall_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//go:build !integration

package scanall

import (
"context"
"testing"

"github.com/mittwald/goharbor-client/v5/apiv2/internal/api/client/scan_all"
"github.com/mittwald/goharbor-client/v5/apiv2/mocks"
"github.com/mittwald/goharbor-client/v5/apiv2/model"
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{
ScanAll: mocks.MockScan_allClientService{},
ProjectMetadata: mocks.MockProject_metadataClientService{},
}

v2Client := clienttesting.BuildV2ClientWithMocks(desiredMockClients)

cl := NewClient(v2Client, clienttesting.DefaultOpts, clienttesting.AuthInfo)

return cl, desiredMockClients
}

func TestRESTClient_CreateScanAllSchedule(t *testing.T) {
apiClient, mockClient := APIandMockClientsForTests()

params := &scan_all.CreateScanAllScheduleParams{
Context: ctx,
Schedule: &model.Schedule{
Schedule: &model.ScheduleObj{
Type: "Daily",
},
},
}

params.WithTimeout(apiClient.Options.Timeout)

mockClient.ScanAll.On("CreateScanAllSchedule", params, mock.AnythingOfType("runtime.ClientAuthInfoWriterFunc")).
Return(&scan_all.CreateScanAllScheduleCreated{}, nil)

schedule := &model.Schedule{
Schedule: &model.ScheduleObj{
Type: "Daily",
},
}

err := apiClient.CreateScanAllSchedule(ctx, schedule)
require.NoError(t, err)

mockClient.Retention.AssertExpectations(t)
}

func TestRESTClient_GetScanAllSchedule(t *testing.T) {
apiClient, mockClient := APIandMockClientsForTests()

params := &scan_all.GetScanAllScheduleParams{
Context: ctx,
}

params.WithTimeout(apiClient.Options.Timeout)

mockClient.ScanAll.On("GetScanAllSchedule", params, mock.AnythingOfType("runtime.ClientAuthInfoWriterFunc")).
Return(&scan_all.GetScanAllScheduleOK{
Payload: &model.Schedule{Schedule: &model.ScheduleObj{
Type: "Daily",
},
},
}, nil)
_, err := apiClient.GetScanAllSchedule(ctx)
require.NoError(t, err)

mockClient.Retention.AssertExpectations(t)
}

func TestRESTClient_UpdateScanAllSchedule(t *testing.T) {
apiClient, mockClient := APIandMockClientsForTests()

params := &scan_all.UpdateScanAllScheduleParams{
Context: ctx,
Schedule: &model.Schedule{
Schedule: &model.ScheduleObj{
Type: "Daily",
},
},
}

params.WithTimeout(apiClient.Options.Timeout)

mockClient.ScanAll.On("UpdateScanAllSchedule", params, mock.AnythingOfType("runtime.ClientAuthInfoWriterFunc")).
Return(&scan_all.UpdateScanAllScheduleOK{}, nil)

schedule := &model.Schedule{
Schedule: &model.ScheduleObj{
Type: "Daily",
},
}

err := apiClient.UpdateScanAllSchedule(ctx, schedule)
require.NoError(t, err)

mockClient.Retention.AssertExpectations(t)
}
Loading

0 comments on commit 69162b1

Please sign in to comment.