Skip to content

Commit 92d8f8c

Browse files
authored
feat(policy): list attrs by namespace (#479)
1 parent 93e09fb commit 92d8f8c

File tree

9 files changed

+421
-372
lines changed

9 files changed

+421
-372
lines changed

docs/grpc/index.html

Lines changed: 7 additions & 0 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: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

integration/attribute_values_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ func (s *AttributeValuesSuite) Test_DeactivateAttribute_Cascades_List() {
403403
}
404404

405405
listAttributes := func(state string) bool {
406-
listedAttrs, err := s.db.PolicyClient.ListAllAttributes(s.ctx, state)
406+
listedAttrs, err := s.db.PolicyClient.ListAllAttributes(s.ctx, state, "")
407407
assert.Nil(s.T(), err)
408408
assert.NotNil(s.T(), listedAttrs)
409409
for _, a := range listedAttrs {

integration/attributes_test.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ func (s *AttributesSuite) Test_GetAttribute_Deactivated_Succeeds() {
229229
func (s *AttributesSuite) Test_ListAttribute() {
230230
fixtures := s.getAttributeFixtures()
231231

232-
list, err := s.db.PolicyClient.ListAllAttributes(s.ctx, policydb.StateActive)
232+
list, err := s.db.PolicyClient.ListAllAttributes(s.ctx, policydb.StateActive, "")
233233
assert.Nil(s.T(), err)
234234
assert.NotNil(s.T(), list)
235235

@@ -246,6 +246,24 @@ func (s *AttributesSuite) Test_ListAttribute() {
246246
}
247247
}
248248

249+
func (s *AttributesSuite) Test_ListAttributesByNamespace() {
250+
// get all unique namespace_ids
251+
nsIds := map[string]bool{}
252+
for _, f := range s.getAttributeFixtures() {
253+
nsIds[f.NamespaceId] = true
254+
}
255+
// get all attributes by namespace
256+
for nsId := range nsIds {
257+
list, err := s.db.PolicyClient.ListAllAttributes(s.ctx, policydb.StateAny, nsId)
258+
assert.Nil(s.T(), err)
259+
assert.NotNil(s.T(), list)
260+
assert.NotEmpty(s.T(), list)
261+
for _, l := range list {
262+
assert.Equal(s.T(), nsId, l.Namespace.Id)
263+
}
264+
}
265+
}
266+
249267
func (s *AttributesSuite) Test_UpdateAttribute() {
250268
fixedLabel := "fixed label"
251269
updateLabel := "update label"
@@ -452,7 +470,7 @@ func (s *AttributesSuite) Test_DeactivateAttribute_Cascades_List() {
452470
}
453471

454472
listAttributes := func(state string) bool {
455-
listedAttrs, err := s.db.PolicyClient.ListAllAttributes(s.ctx, state)
473+
listedAttrs, err := s.db.PolicyClient.ListAllAttributes(s.ctx, state, "")
456474
assert.Nil(s.T(), err)
457475
assert.NotNil(s.T(), listedAttrs)
458476
for _, a := range listedAttrs {

integration/namespaces_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ func (s *NamespacesSuite) Test_DeactivateNamespace_Cascades_List() {
297297
}
298298

299299
listAttributes := func(state string) bool {
300-
listedAttrs, err := s.db.PolicyClient.ListAllAttributes(s.ctx, state)
300+
listedAttrs, err := s.db.PolicyClient.ListAllAttributes(s.ctx, state, "")
301301
assert.Nil(s.T(), err)
302302
assert.NotNil(s.T(), listedAttrs)
303303
for _, a := range listedAttrs {

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

Lines changed: 376 additions & 366 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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,10 +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
5253
slog.Debug("listing attribute definitions", slog.String("state", state))
5354
rsp := &attributes.ListAttributesResponse{}
5455

55-
list, err := s.dbClient.ListAllAttributes(ctx, state)
56+
list, err := s.dbClient.ListAllAttributes(ctx, state, namespace_id)
5657
if err != nil {
5758
return nil, services.HandleError(err, services.ErrListRetrievalFailed)
5859
}

services/policy/attributes/attributes.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ message ValueKeyAccessServer {
2929
message ListAttributesRequest {
3030
// ACTIVE by default when not specified
3131
common.ActiveStateEnum state = 1;
32+
string namespace_id = 2;
3233
}
3334
message ListAttributesResponse {
3435
repeated policy.Attribute attributes = 1;

services/policy/db/attributes.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ type attributesSelectOptions struct {
5959
withFqn bool
6060
withOneValueByFqn string
6161
state string
62+
namespace_id string
6263
}
6364

6465
func attributesSelect(opts attributesSelectOptions) sq.SelectBuilder {
@@ -274,15 +275,20 @@ func listAllAttributesSql(opts attributesSelectOptions) (string, []interface{},
274275
if opts.state != "" && opts.state != StateAny {
275276
sb = sb.Where(sq.Eq{t.Field("active"): opts.state == StateActive})
276277
}
278+
279+
if opts.namespace_id != "" {
280+
sb = sb.Where(sq.Eq{t.Field("namespace_id"): opts.namespace_id})
281+
}
277282
return sb.ToSql()
278283
}
279284

280-
func (c PolicyDbClient) ListAllAttributes(ctx context.Context, state string) ([]*policy.Attribute, error) {
285+
func (c PolicyDbClient) ListAllAttributes(ctx context.Context, state string, namespace_id string) ([]*policy.Attribute, error) {
281286
opts := attributesSelectOptions{
282287
withAttributeValues: true,
283288
withKeyAccessGrants: false,
284289
withFqn: true,
285290
state: state,
291+
namespace_id: namespace_id,
286292
}
287293

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

0 commit comments

Comments
 (0)