Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add immutable rules to projects #190

Merged
merged 2 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions apiv2/pkg/clients/immutable/immutable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package immutable

import (
"context"
immutableapi "github.com/mittwald/goharbor-client/v5/apiv2/internal/api/client/immutable"
"github.com/mittwald/goharbor-client/v5/apiv2/pkg/config"

v2client "github.com/mittwald/goharbor-client/v5/apiv2/internal/api/client"
"github.com/mittwald/goharbor-client/v5/apiv2/model"

"github.com/go-openapi/runtime"
)

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 {
CreateImmuRule(ctx context.Context, projectNameOrID string, immutableRule *model.ImmutableRule) error
UpdateImmuRule(ctx context.Context, projectNameOrID string, immutableRule *model.ImmutableRule) error
DeleteImmuRule(ctx context.Context, projectNameOrID string, immutableRuleID int64) error
ListImmuRules(ctx context.Context, projectNameOrID string) ([]*model.ImmutableRule, error)
}

func (c *RESTClient) CreateImmuRule(ctx context.Context, projectNameOrID string, immutableRule *model.ImmutableRule) error {
params := &immutableapi.CreateImmuRuleParams{
ProjectNameOrID: projectNameOrID,
ImmutableRule: immutableRule,
Context: ctx,
}

params.WithTimeout(c.Options.Timeout)

_, err := c.V2Client.Immutable.CreateImmuRule(params, c.AuthInfo)
if err != nil {
return handleSwaggerImmutableRuleErrors(err)
}

return nil
}

func (c *RESTClient) UpdateImmuRule(ctx context.Context, projectNameOrID string, immutableRule *model.ImmutableRule, immutableRuleID int64) error {
params := &immutableapi.UpdateImmuRuleParams{
ProjectNameOrID: projectNameOrID,
ImmutableRule: immutableRule,
ImmutableRuleID: immutableRuleID,
Context: ctx,
}

params.WithTimeout(c.Options.Timeout)

params.ImmutableRule.ID = immutableRuleID

_, err := c.V2Client.Immutable.UpdateImmuRule(params, c.AuthInfo)
if err != nil {
return handleSwaggerImmutableRuleErrors(err)
}

return nil
}

func (c *RESTClient) DeleteImmuRule(ctx context.Context, projectNameOrID string, immutableRuleID int64) error {
params := &immutableapi.DeleteImmuRuleParams{
ProjectNameOrID: projectNameOrID,
ImmutableRuleID: immutableRuleID,
Context: ctx,
}

params.WithTimeout(c.Options.Timeout)

_, err := c.V2Client.Immutable.DeleteImmuRule(params, c.AuthInfo)
if err != nil {
return handleSwaggerImmutableRuleErrors(err)
}

return nil
}

func (c *RESTClient) ListImmuRules(ctx context.Context, projectNameOrID string) ([]*model.ImmutableRule, error) {
var immutableRules []*model.ImmutableRule
page := c.Options.Page

params := &immutableapi.ListImmuRulesParams{
Page: &page,
PageSize: &c.Options.PageSize,
ProjectNameOrID: projectNameOrID,
Q: &c.Options.Query,
Sort: &c.Options.Sort,
Context: ctx,
}

params.WithTimeout(c.Options.Timeout)

for {
resp, err := c.V2Client.Immutable.ListImmuRules(params, c.AuthInfo)
if err != nil {
return nil, handleSwaggerImmutableRuleErrors(err)
}

if len(resp.Payload) == 0 {
break
}

totalCount := resp.XTotalCount

immutableRules = append(immutableRules, resp.Payload...)

if int64(len(immutableRules)) >= totalCount {
break
}

page++
}

return immutableRules, nil
}
42 changes: 42 additions & 0 deletions apiv2/pkg/clients/immutable/immutable_errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package immutable

import (
"net/http"

"github.com/go-openapi/runtime"
"github.com/mittwald/goharbor-client/v5/apiv2/internal/api/client/immutable"
"github.com/mittwald/goharbor-client/v5/apiv2/pkg/errors"
)

func handleSwaggerImmutableRuleErrors(in error) error {
t, ok := in.(*runtime.APIError)
if ok {
switch t.Code {
case http.StatusOK:
return nil
case http.StatusCreated:
return nil
case http.StatusBadRequest:
return &errors.ErrBadRequest{}
case http.StatusUnauthorized:
return &errors.ErrUnauthorized{}
case http.StatusForbidden:
return &errors.ErrForbidden{}
case http.StatusNotFound:
return &errors.ErrNotFound{}
case http.StatusInternalServerError:
return &errors.ErrInternalErrors{}
}
}

switch in.(type) {
case *immutable.UpdateImmuRuleBadRequest:
return &errors.ErrBadRequest{}
case *immutable.CreateImmuRuleBadRequest:
return &errors.ErrBadRequest{}
case *immutable.DeleteImmuRuleBadRequest:
return &errors.ErrBadRequest{}
default:
return in
}
}
116 changes: 116 additions & 0 deletions apiv2/pkg/clients/immutable/immutable_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
//go:build integration

package immutable

import (
"context"
"testing"
"github.com/mittwald/goharbor-client/v5/apiv2/model"
"github.com/mittwald/goharbor-client/v5/apiv2/pkg/clients/project"
clienttesting "github.com/mittwald/goharbor-client/v5/apiv2/pkg/testing"
"github.com/stretchr/testify/require"
)

var projectName = "test-project"

func TestAPIImmutableListImmutableRules(t *testing.T) {
ctx := context.Background()

c := NewClient(clienttesting.V2SwaggerClient, clienttesting.DefaultOpts, clienttesting.AuthInfo)
pc := project.NewClient(clienttesting.V2SwaggerClient, clienttesting.DefaultOpts, clienttesting.AuthInfo)

err := pc.NewProject(ctx, &model.ProjectReq{
ProjectName: projectName,
})
require.NoError(t, err)

defer pc.DeleteProject(ctx, projectName)

immutableRule := model.ImmutableRule{
ScopeSelectors: map[string][]model.ImmutableSelector{},
TagSelectors: []*model.ImmutableSelector{{
Decoration: "matches",
Kind: "doublestar",
Pattern: "**",
}},
}

err = c.CreateImmuRule(ctx, projectName, &immutableRule)
require.NoError(t, err)

listedImmutableRules, err := c.ListImmuRules(ctx, projectName)

require.NoError(t, err)

listedImmutableRuleTag := listedImmutableRules[0].TagSelectors

require.Equal(t, listedImmutableRuleTag[0], immutableRule.TagSelectors[0])

immuRuleID := listedImmutableRules[0].ID

c.DeleteImmuRule(ctx, projectName, immuRuleID)

checkDeletedImmuRules, err := c.ListImmuRules(ctx, projectName)

require.Empty(t, checkDeletedImmuRules)
}

func TestAPIImmutableUpdateImmutableRules(t *testing.T) {
ctx := context.Background()

c := NewClient(clienttesting.V2SwaggerClient, clienttesting.DefaultOpts, clienttesting.AuthInfo)
pc := project.NewClient(clienttesting.V2SwaggerClient, clienttesting.DefaultOpts, clienttesting.AuthInfo)

err := pc.NewProject(ctx, &model.ProjectReq{
ProjectName: projectName,
})
require.NoError(t, err)

defer pc.DeleteProject(ctx, projectName)

createImmutableRule := model.ImmutableRule{
ScopeSelectors: map[string][]model.ImmutableSelector{},
TagSelectors: []*model.ImmutableSelector{{
Decoration: "matches",
Kind: "doublestar",
Pattern: "1.0.0",
}},
}

updateImmutableRule := model.ImmutableRule{
ScopeSelectors: map[string][]model.ImmutableSelector{},
TagSelectors: []*model.ImmutableSelector{{
Decoration: "matches",
Kind: "doublestar",
Pattern: "2.0.0",
}},
}

err = c.CreateImmuRule(ctx, projectName, &createImmutableRule)
require.NoError(t, err)

listedImmutableRules, err := c.ListImmuRules(ctx, projectName)

require.NoError(t, err)

immuRuleID := listedImmutableRules[0].ID

err = c.UpdateImmuRule(ctx, projectName, &updateImmutableRule, immuRuleID)

require.NoError(t, err)

listedImmutableRules, err = c.ListImmuRules(ctx, projectName)

require.NoError(t, err)

listedImmutableRuleTag := listedImmutableRules[0].TagSelectors

require.Equal(t, listedImmutableRuleTag[0], updateImmutableRule.TagSelectors[0])


c.DeleteImmuRule(ctx, projectName, immuRuleID)

checkDeletedImmuRules, err := c.ListImmuRules(ctx, projectName)

require.Empty(t, checkDeletedImmuRules)
}
Loading
Loading