Skip to content

Commit 04e723f

Browse files
authored
feat(policy): list attrs by namespace name (#487)
1 parent 8435f59 commit 04e723f

File tree

7 files changed

+411
-390
lines changed

7 files changed

+411
-390
lines changed

docs/grpc/index.html

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/openapi/policy/attributes/attributes.swagger.json

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

integration/attributes_test.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,19 +248,31 @@ func (s *AttributesSuite) Test_ListAttribute() {
248248

249249
func (s *AttributesSuite) Test_ListAttributesByNamespace() {
250250
// get all unique namespace_ids
251-
nsIds := map[string]bool{}
251+
namespaces := map[string]string{}
252252
for _, f := range s.getAttributeFixtures() {
253-
nsIds[f.NamespaceId] = true
253+
namespaces[f.NamespaceId] = ""
254254
}
255-
// get all attributes by namespace
256-
for nsId := range nsIds {
255+
// list attributes by namespace id
256+
for nsId := range namespaces {
257257
list, err := s.db.PolicyClient.ListAllAttributes(s.ctx, policydb.StateAny, nsId)
258258
assert.Nil(s.T(), err)
259259
assert.NotNil(s.T(), list)
260260
assert.NotEmpty(s.T(), list)
261261
for _, l := range list {
262262
assert.Equal(s.T(), nsId, l.Namespace.Id)
263263
}
264+
namespaces[nsId] = list[0].Namespace.Name
265+
}
266+
267+
// list attributes by namespace name
268+
for _, nsName := range namespaces {
269+
list, err := s.db.PolicyClient.ListAllAttributes(s.ctx, policydb.StateAny, nsName)
270+
assert.Nil(s.T(), err)
271+
assert.NotNil(s.T(), list)
272+
assert.NotEmpty(s.T(), list)
273+
for _, l := range list {
274+
assert.Equal(s.T(), nsName, l.Namespace.Name)
275+
}
264276
}
265277
}
266278

protocol/go/policy/attributes/attributes.pb.go

Lines changed: 376 additions & 375 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

services/policy/attributes/attributes.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ func (s *AttributesService) ListAttributes(ctx context.Context,
4949
req *attributes.ListAttributesRequest,
5050
) (*attributes.ListAttributesResponse, error) {
5151
state := services.GetDbStateTypeTransformedEnum(req.State)
52-
namespace_id := req.NamespaceId
52+
namespace := req.Namespace
5353
slog.Debug("listing attribute definitions", slog.String("state", state))
5454
rsp := &attributes.ListAttributesResponse{}
5555

56-
list, err := s.dbClient.ListAllAttributes(ctx, state, namespace_id)
56+
list, err := s.dbClient.ListAllAttributes(ctx, state, namespace)
5757
if err != nil {
5858
return nil, services.HandleError(err, services.ErrListRetrievalFailed)
5959
}

services/policy/attributes/attributes.proto

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ message ValueKeyAccessServer {
2929
message ListAttributesRequest {
3030
// ACTIVE by default when not specified
3131
common.ActiveStateEnum state = 1;
32-
string namespace_id = 2;
32+
// can be id or name
33+
string namespace = 2;
3334
}
3435
message ListAttributesResponse {
3536
repeated policy.Attribute attributes = 1;

services/policy/db/attributes.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010

1111
sq "github.com/Masterminds/squirrel"
12+
"github.com/google/uuid"
1213
"github.com/jackc/pgx/v5"
1314
"github.com/opentdf/platform/internal/db"
1415
"github.com/opentdf/platform/protocol/go/common"
@@ -59,7 +60,7 @@ type attributesSelectOptions struct {
5960
withFqn bool
6061
withOneValueByFqn string
6162
state string
62-
namespace_id string
63+
namespace string
6364
}
6465

6566
func attributesSelect(opts attributesSelectOptions) sq.SelectBuilder {
@@ -276,19 +277,24 @@ func listAllAttributesSql(opts attributesSelectOptions) (string, []interface{},
276277
sb = sb.Where(sq.Eq{t.Field("active"): opts.state == StateActive})
277278
}
278279

279-
if opts.namespace_id != "" {
280-
sb = sb.Where(sq.Eq{t.Field("namespace_id"): opts.namespace_id})
280+
if opts.namespace != "" {
281+
_, err := uuid.Parse(opts.namespace)
282+
if err == nil {
283+
sb = sb.Where(sq.Eq{t.Field("namespace_id"): opts.namespace})
284+
} else {
285+
sb = sb.Where(sq.Eq{Tables.Namespaces.Field("name"): opts.namespace})
286+
}
281287
}
282288
return sb.ToSql()
283289
}
284290

285-
func (c PolicyDbClient) ListAllAttributes(ctx context.Context, state string, namespace_id string) ([]*policy.Attribute, error) {
291+
func (c PolicyDbClient) ListAllAttributes(ctx context.Context, state string, namespace string) ([]*policy.Attribute, error) {
286292
opts := attributesSelectOptions{
287293
withAttributeValues: true,
288294
withKeyAccessGrants: false,
289295
withFqn: true,
290296
state: state,
291-
namespace_id: namespace_id,
297+
namespace: namespace,
292298
}
293299

294300
sql, args, err := listAllAttributesSql(opts)

0 commit comments

Comments
 (0)