Skip to content

Add Count RPC to EndDevice registry#7909

Open
happyRip wants to merge 1 commit into
v3.36from
feature/end-device-count
Open

Add Count RPC to EndDevice registry#7909
happyRip wants to merge 1 commit into
v3.36from
feature/end-device-count

Conversation

@happyRip
Copy link
Copy Markdown
Member

@happyRip happyRip commented May 7, 2026

Summary

References https://github.com/TheThingsIndustries/lorawan-stack/issues/4802

Changes

  • Add Count RPC to EndDeviceRegistry with REST endpoint GET /applications/{id}/devices/count.
    • Reuses existing CountEndDevices store method; requires RIGHT_APPLICATION_DEVICES_READ.

Testing

Steps
  1. Register an application with some end devices.
  2. Call GET /applications/{app_id}/devices/count with valid auth. Verify response {"count": N}.
  3. Call without RIGHT_APPLICATION_DEVICES_READ and verify permission denied.
Results

Read device count

$ curl -sk \
  -H "Authorization: Bearer $API_KEY" \
  'https://localhost:8885/api/v3/applications/count-test-app-1778171996/devices/count'

returns

{"count":2}%

Use an API key with no read permission

$ curl -sk \
  -H "Authorization: Bearer $API_KEY_NO_READ" \
  'https://localhost:8885/api/v3/applications/count-test-app-1778171996/devices/count'

returns

{
  "code": 7,
  "message": "error:pkg/auth/rights:insufficient_application_rights (insufficient rights for application `count-test-app-1778171996`)",
  "details": [
    {
      "@type": "type.googleapis.com/ttn.lorawan.v3.ErrorDetails",
      "namespace": "pkg/auth/rights",
      "name": "insufficient_application_rights",
      "message_format": "insufficient rights for application `{uid}`",
      "attributes": {
        "missing": [
          "RIGHT_APPLICATION_DEVICES_READ"
        ],
        "uid": "count-test-app-1778171996"
      },
      "correlation_id": "3f2697171ad7429bbccba2e878ce960c",
      "code": 7
    }
  ]
}
Regressions

Existing List and Delete device endpoints are unchanged. Store layer CountEndDevices was already tested.

Notes for Reviewers

The store interface and bunstore implementation for CountEndDevices already existed (used by deleteApplication/purgeApplication). This PR only adds the proto definition and gRPC handler to expose it as a public API.

Checklist

  • Scope: The referenced issue is addressed, there are no unrelated changes.
  • Compatibility: The changes are backwards compatible with existing API, storage, configuration and CLI, according to the compatibility commitments in README.md for the chosen target branch.
  • Documentation: Relevant documentation is added or updated.
  • Testing: The steps/process to test this feature are clearly explained including testing for regressions.
  • Infrastructure: If infrastructural changes (e.g., new RPC, configuration) are needed, a separate issue is created in the infrastructural repositories.
  • Changelog: Significant features, behavior changes, deprecations and fixes are added to CHANGELOG.md.
  • Commits: Commit messages follow guidelines in CONTRIBUTING.md, there are no fixup commits left.

@happyRip happyRip added this to the v3.36.1 milestone May 7, 2026
@happyRip happyRip self-assigned this May 7, 2026
@github-actions github-actions Bot added the c/identity server This is related to the Identity Server label May 7, 2026
@happyRip happyRip force-pushed the feature/end-device-count branch 4 times, most recently from 6f138b1 to 202f100 Compare May 7, 2026 16:16
@happyRip happyRip marked this pull request as ready for review May 11, 2026 10:36
@happyRip happyRip requested review from a team as code owners May 11, 2026 10:36
@happyRip happyRip requested review from halimi, johanstokking and ryaplots and removed request for halimi May 11, 2026 10:36
@happyRip
Copy link
Copy Markdown
Member Author

Imre is on leave (Stack Reviewers 1)

@happyRip happyRip requested a review from nicholaspcr May 11, 2026 11:05
Copy link
Copy Markdown
Member

@johanstokking johanstokking left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API and implementation look good but this is missing tests.

Copy link
Copy Markdown
Contributor

@nicholaspcr nicholaspcr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few points:

  • There should be a CHANGELOG entry
  • Is it worth considering filters to this new endpoint?
    • created_since or other fields associated with IS device fields.

Comment thread api/ttn/lorawan/v3/end_device.proto Outdated
}

message CountEndDevicesResponse {
uint32 count = 1;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why uint32 instead of uint64, considering that:

  • The store implementation returns uint64
  • The value we set in the total header (on any List requests) is derived from the uint64.

Comment on lines +336 to +356
func (is *IdentityServer) countEndDevices(
ctx context.Context, req *ttnpb.CountEndDevicesRequest,
) (*ttnpb.CountEndDevicesResponse, error) {
if err := rights.RequireApplication(
ctx, req.GetApplicationIds(), ttnpb.Right_RIGHT_APPLICATION_DEVICES_READ,
); err != nil {
return nil, err
}
var count uint64
err := is.store.Transact(ctx, func(ctx context.Context, st store.Store) (err error) {
count, err = st.CountEndDevices(ctx, req.GetApplicationIds())
return err
})
if err != nil {
return nil, err
}
return &ttnpb.CountEndDevicesResponse{
Count: uint32(min(count, math.MaxUint32)), // #nosec G115 -- bounded by min
}, nil
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a unit test for this in the end_device_registry_test.go

@happyRip happyRip force-pushed the feature/end-device-count branch from 202f100 to cedda37 Compare May 12, 2026 13:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c/identity server This is related to the Identity Server

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants