Skip to content

refactor: add GET /api/v1/devices/export endpoint - #1209

Draft
ShradhaGupta31 wants to merge 1 commit into
mainfrom
device-discovery-exportAPI
Draft

refactor: add GET /api/v1/devices/export endpoint#1209
ShradhaGupta31 wants to merge 1 commit into
mainfrom
device-discovery-exportAPI

Conversation

@ShradhaGupta31

@ShradhaGupta31 ShradhaGupta31 commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

Addresses: #1076

ADR: https://github.com/device-management-toolkit/console/wiki/ADR-devices-export-api

  • flat->nested mapping done in one place so SQL and Mongo responses match
  • me/os/platform/bmc render as null when a subsystem has no data
  • X-Total-Count header and server-side audit log per export attempt
  • fields with no writer yet are marked export-map and render null

- flat->nested mapping done in one place so SQL and Mongo responses match
- me/os/platform/bmc render as null when a subsystem has no data
- X-Total-Count header and server-side audit log per export attempt
- fields with no writer yet are marked export-map and render null

Add the Fuego/OpenAPI declaration and a Postman request asserting the
envelope shape, X-Total-Count, and absence of credential fields.

Signed-off-by: ShradhaGupta31 <[email protected]>
@codecov

codecov Bot commented Aug 19, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 54.35897% with 89 lines in your changes missing coverage. Please review.
✅ Project coverage is 50.19%. Comparing base (47d9e22) to head (10f750f).

Files with missing lines Patch % Lines
internal/controller/openapi/devices.go 0.00% 77 Missing ⚠️
internal/controller/httpapi/v1/export.go 89.74% 10 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1209      +/-   ##
==========================================
+ Coverage   50.13%   50.19%   +0.05%     
==========================================
  Files         147      148       +1     
  Lines       13574    13769     +195     
==========================================
+ Hits         6805     6911     +106     
- Misses       6176     6263      +87     
- Partials      593      595       +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.


outcomeSuccess = "success"
outcomeError = "error"
tenantClaimKey = "tenantId"

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.

This is not needed, the way tenantId is obtained is out of scope for this PR. In console we are planning to get the tenantid through x-tenant-id header. Refer this PR: #1220

ctx, cancel := context.WithTimeout(c.Request.Context(), exportTimeout)
defer cancel()

devicesList, err := dr.t.Get(ctx, maxExportRecords, 0, tenantID)

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.

For now pass tenantID as "" through PR: #1220 will update this correctly


// exportIdentity reads the tenant and subject from the already-verified JWT. The
// export is tenant-scoped: only devices for the caller's tenant are returned
func exportIdentity(c *gin.Context) (tenantID, userID string) {

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.

This function is not needed

h := handler.Group("/devices")
{
h.GET("", r.get)
h.GET("export", r.export)

@sudhir-intc sudhir-intc Aug 26, 2026

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.

The GET /api/v1/devices/export approach could work as an initial implementation for relatively small datasets where the export can be generated within the lifetime of a single HTTP request. We could also start with an explicit limit on the number of devices that can be exported for ex: 500.

However, we should consider how this approach would evolve when the number of devices grows significantly or when filters result in a long-running query. In such cases, a synchronous request may lead to long request durations, timeouts, and increased resource consumption.

To avoid having to change the API contract later, it may be worth starting with the export modeled as a separate resource which you are already doing, while initially keeping the underlying implementation simple.
For future we need to plan implementing the export resource with API's for ex:

# Creates an export request with the required filters and returns an export ID. Initially, the export could be processed by an in-process/background worker.
POST   /api/v1/device-exports
# Retrieves the status and metadata of the export job, such as queued, running, completed, or failed.
GET    /api/v1/device-exports/{id}
# Downloads the generated export file once the export has completed.
GET    /api/v1/device-exports/{id}/file
# Cancels an in-progress export and/or removes the export job and generated file, depending on the defined lifecycle.
DELETE /api/v1/device-exports/{id}

This would allow us to incrementally evolve the implementation without changing the external API. For example, we could initially support small exports using an in-process worker and local storage, and later add batch processing, persistent job state, cancellation, cleanup, distributed workers, and object storage as scalability requirements evolve.

This approach gives us something simple to implement initially while providing a clearer path for supporting larger datasets and container/Kubernetes deployments in the future.


const (
// maxExportRecords is the hard cap on records returned by a single export.
maxExportRecords = 10000

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.

10000 seems to be a big number we would reduce it to may be 500 or so. Please see my comment in the devices.go

exportTimeout = 60 * time.Second

// exportCountHeader tells the client how many records are in the response.
exportCountHeader = "X-Total-Count"

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 do we need an header ? This should be captured in the response body

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants