-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #180 from sergimrz/add/scanAll-client
add scanall client
- Loading branch information
Showing
7 changed files
with
377 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.