Skip to content

Commit

Permalink
add permv2 migration
Browse files Browse the repository at this point in the history
  • Loading branch information
franzmueller committed Nov 26, 2024
1 parent 6264c63 commit 2312735
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
26 changes: 26 additions & 0 deletions lib/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
deviceRepo "github.com/SENERGY-Platform/device-repository/lib/client"
"github.com/SENERGY-Platform/import-repository/lib/config"
"github.com/SENERGY-Platform/import-repository/lib/database"
"github.com/SENERGY-Platform/import-repository/lib/model"
permV2 "github.com/SENERGY-Platform/permissions-v2/pkg/client"
)

Expand Down Expand Up @@ -59,3 +60,28 @@ type Controller struct {
func getTimeoutContext() (context.Context, context.CancelFunc) {
return context.WithTimeout(context.Background(), 10*time.Second)
}

func (this *Controller) Migrate() error {
ctx, _ := getTimeoutContext()
importTypes, _, err := this.db.ListImportTypes(ctx, model.ImportTypeListOptions{})
if err != nil {
return err
}
for _, importType := range importTypes {
resource, err, _ := this.permV2Client.GetResource(permV2.InternalAdminToken, PermV2Topic, importType.Id)
if err != nil {
return err
}
resource.UserPermissions[importType.Owner] = permV2.PermissionsMap{
Read: true,
Write: true,
Execute: true,
Administrate: true,
}
_, err, _ = this.permV2Client.SetPermission(permV2.InternalAdminToken, PermV2Topic, importType.Id, resource.ResourcePermissions)
if err != nil {
return err
}
}
return nil
}
5 changes: 0 additions & 5 deletions lib/controller/import_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@ func (this *Controller) CreateImportType(importType model.ImportType, token jwt.
}

func (this *Controller) ReadImportType(id string, token jwt.Token) (result model.ImportType, err error, errCode int) {
err, code := this.CheckAccessToImportType(token, id, permV2Model.Read)
if err != nil {
result = model.ImportType{}
return result, err, code
}
ctx, _ := getTimeoutContext()
result, exists, err := this.db.GetImportType(ctx, id)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions lib/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ func StartWithPermv2Client(conf config.Config, ctx context.Context, wg *sync.Wai
return err
}

err = ctrl.Migrate()
if err != nil {
log.Println("ERROR: unable to migrate", err)
return err
}

_, err = consumer.NewConsumer(ctx, wg, conf.KafkaBootstrap, []string{conf.UsersTopic}, conf.GroupId, consumer.Earliest,
listener.UsersListenerFactory(ctrl), consumer.HandleError, conf.Debug)
if err != nil {
Expand Down

0 comments on commit 2312735

Please sign in to comment.