Skip to content
Open
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
24 changes: 24 additions & 0 deletions pkg/api/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"context"

"github.com/google/uuid"
"github.com/permitio/permit-golang/pkg/config"
"github.com/permitio/permit-golang/pkg/errors"
Expand Down Expand Up @@ -167,6 +168,29 @@ func (r *Resources) Update(ctx context.Context, resourceKey string, resourceUpda
return resource, nil
}

// Replace a resource.
// Usage Example:
// ```
//
// resourceReplace := models.NewResourceReplace("Document", map[string]models.ActionBlockEditable{"read": {}, "write": {}}
// resource, err := PermitClient.Api.Resources.Replace(ctx, "document", resourceReplace)
//
// ```
func (r *Resources) Replace(ctx context.Context, resourceKey string, resourceReplace models.ResourceReplace) (*models.ResourceRead, error) {
err := r.lazyLoadPermitContext(ctx)
if err != nil {
r.logger.Error("", zap.Error(err))
return nil, err
}
resource, httpRes, err := r.client.ResourcesApi.ReplaceResource(ctx, r.config.Context.GetProject(), r.config.Context.GetEnvironment(), resourceKey).ResourceReplace(resourceReplace).Execute()
err = errors.HttpErrorHandle(err, httpRes)
if err != nil {
r.logger.Error("error replacing resource: "+resourceKey, zap.Error(err))
return nil, err
}
return resource, nil
}

// Delete a resource.
// Usage Example:
//
Expand Down
21 changes: 15 additions & 6 deletions pkg/tests/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ package tests
import (
"context"
"fmt"
"math/rand"
"os"
"reflect"
"testing"
"time"

"github.com/permitio/permit-golang/pkg/config"
"github.com/permitio/permit-golang/pkg/enforcement"
PermitErrors "github.com/permitio/permit-golang/pkg/errors"
"github.com/permitio/permit-golang/pkg/models"
"github.com/permitio/permit-golang/pkg/permit"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"math/rand"
"os"
"reflect"
"testing"
"time"
)

var runId = randId()
Expand Down Expand Up @@ -211,6 +212,14 @@ func TestIntegration(t *testing.T) {
_, err = permitClient.Api.Resources.Create(ctx, resourceCreate)
assert.NoError(t, err)

resourceReplace := *models.NewResourceReplace(resourceKey+"-2",
map[string]models.ActionBlockEditable{
"read": {Attributes: map[string]interface{}{"marker": marker}},
"write": {Attributes: map[string]interface{}{"marker": marker}},
})
_, err = permitClient.Api.Resources.Replace(ctx, resourceKey+"-2", resourceReplace)
assert.NoError(t, err)

list, err := permitClient.Api.Resources.Search(ctx, 1, 100, resourceKey)
assert.NoError(t, err)
assert.Len(t, list, 2)
Expand Down Expand Up @@ -306,7 +315,7 @@ func TestIntegration(t *testing.T) {

userPermissions, err := permitClient.GetUserPermissions(enforcement.UserBuilder(userKey).Build())
assert.NoError(t, err)
userPermissionsInTenant, found := userPermissions["__tenant:" + tenantKey]
userPermissionsInTenant, found := userPermissions["__tenant:"+tenantKey]
assert.True(t, found)
assert.Equal(t, tenantKey, userPermissionsInTenant.Tenant.Key)
assert.True(t, assert.ObjectsAreEqual(tenantCreate.Attributes, userPermissionsInTenant.Tenant.Attributes))
Expand Down