Skip to content

Commit

Permalink
feat: add policy CRUD warpper
Browse files Browse the repository at this point in the history
Signed-off-by: 1998-felix <[email protected]>
  • Loading branch information
felixgateru committed Aug 28, 2024
1 parent a462d17 commit f401254
Show file tree
Hide file tree
Showing 7 changed files with 551 additions and 12 deletions.
5 changes: 4 additions & 1 deletion cmd/things/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
gevents "github.com/absmach/magistrala/internal/groups/events"
gpostgres "github.com/absmach/magistrala/internal/groups/postgres"
gtracing "github.com/absmach/magistrala/internal/groups/tracing"
mgpolicy "github.com/absmach/magistrala/internal/policy"
mglog "github.com/absmach/magistrala/logger"
"github.com/absmach/magistrala/pkg/groups"
"github.com/absmach/magistrala/pkg/grpcclient"
Expand Down Expand Up @@ -242,7 +243,9 @@ func newService(ctx context.Context, db *sqlx.DB, dbConfig pgclient.Config, auth

thingCache := thcache.NewCache(cacheClient, keyDuration)

csvc := things.NewService(authClient, policyClient, cRepo, gRepo, thingCache, idp)
policyService := mgpolicy.NewService(policyClient)

csvc := things.NewService(authClient, policyService, cRepo, gRepo, thingCache, idp)
gsvc := mggroups.NewService(gRepo, idp, authClient, policyClient)

csvc, err := thevents.NewEventStoreMiddleware(ctx, csvc, esURL)
Expand Down
126 changes: 126 additions & 0 deletions internal/policy/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0

package policy

import (
"context"

"github.com/absmach/magistrala"
"github.com/absmach/magistrala/pkg/policy"
)

type service struct {
policy magistrala.PolicyServiceClient
}

func NewService(policy magistrala.PolicyServiceClient) policy.Service {
return &service{
policy: policy,
}
}

func (s *service) AddPolicy(ctx context.Context, req *magistrala.AddPolicyReq) (bool, error) {
res, err := s.policy.AddPolicy(ctx, req)
if err != nil {
return false, err
}

return res.GetAdded(), nil
}

func (svc *service) AddPolicies(ctx context.Context, req *magistrala.AddPoliciesReq) (bool, error) {
res, err := svc.policy.AddPolicies(ctx, req)
if err != nil {
return false, err
}

return res.GetAdded(), nil
}
func (s *service) DeletePolicyFilter(ctx context.Context, req *magistrala.DeletePolicyFilterReq) (bool, error) {
res, err := s.policy.DeletePolicyFilter(ctx, req)
if err != nil {
return false, err
}
return res.GetDeleted(), nil
}

func (s *service) DeletePolicies(ctx context.Context, req *magistrala.DeletePoliciesReq) (bool, error) {
res, err := s.policy.DeletePolicies(ctx, req)
if err != nil {
return false, err
}
return res.GetDeleted(), nil
}

func (s *service) ListObjects(ctx context.Context, req *magistrala.ListObjectsReq) ([]string, error) {
res, err := s.policy.ListObjects(ctx, req)
if err != nil {
return nil, err
}

return res.Policies, nil
}

func (s *service) ListAllObjects(ctx context.Context, req *magistrala.ListObjectsReq) ([]string, error) {
res, err := s.policy.ListAllObjects(ctx, req)
if err != nil {
return nil, err
}

return res.Policies, nil
}

func (s *service) CountObjects(ctx context.Context, req *magistrala.CountObjectsReq) (uint64, error) {
res, err := s.policy.CountObjects(ctx, req)
if err != nil {
return 0, err
}

return res.Count, nil
}

func (s *service) ListSubjects(ctx context.Context, req *magistrala.ListSubjectsReq) ([]string, error) {
res, err := s.policy.ListSubjects(ctx, req)
if err != nil {
return nil, err
}

return res.Policies, nil
}

func (s *service) ListAllSubjects(ctx context.Context, req *magistrala.ListSubjectsReq) ([]string, error) {
res, err := s.policy.ListAllSubjects(ctx, req)
if err != nil {
return nil, err
}

return res.Policies, nil
}

func (s *service) CountSubjects(ctx context.Context, req *magistrala.CountSubjectsReq) (uint64, error) {
res, err := s.policy.CountSubjects(ctx, req)
if err != nil {
return 0, err
}

return res.Count, nil
}

func (s *service) ListPermissions(ctx context.Context, req *magistrala.ListPermissionsReq) ([]string, error) {
res, err := s.policy.ListPermissions(ctx, req)
if err != nil {
return nil, err
}

return res.GetPermissions(), nil
}

func (s *service) DeleteEntityPolicies(ctx context.Context, req *magistrala.DeleteEntityPoliciesReq) (bool, error) {
res, err := s.policy.DeleteEntityPolicies(ctx, req)
if err != nil {
return false, err
}

return res.GetDeleted(), nil
}
5 changes: 5 additions & 0 deletions pkg/policy/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0

// Package policy contains Magistrala policy definitions.
package policy
Loading

0 comments on commit f401254

Please sign in to comment.