Skip to content

Implement GET /apps/{app-id}/belongs-to endpoint #196

@bburda

Description

@bburda

Summary

Add a new discovery endpoint that returns the Area to which a given App belongs. This is determined by finding the App's parent Component and then looking up which Area that Component belongs to.


Proposed solution

GET /api/v1/apps/{app-id}/belongs-to

Returns the Area that contains this App (via its parent Component's area assignment).

Path parameters:

Parameter Type Required Description
app-id string Yes App identifier (alphanumeric, underscores, hyphens, max 256 chars)

Request body: None

Response 200 OK:

{
  "items": [
    {
      "id": "powertrain",
      "name": "Powertrain",
      "href": "/api/v1/areas/powertrain"
    }
  ],
  "_links": {
    "self": "/api/v1/apps/{app-id}/belongs-to",
    "app": "/api/v1/apps/{app-id}"
  }
}

The items array will contain exactly 0 or 1 area. It may be empty if:

  • The App has no parent Component (component_id is empty)
  • The parent Component has no assigned Area

Response 400 Bad Request:

Returned when the app-id format is invalid.

{
  "error_code": "invalid-parameter",
  "message": "Invalid app ID",
  "parameters": {
    "app_id": "bad id!"
  }
}

Response 404 Not Found:

Returned when no App with the given ID exists.

{
  "error_code": "entity-not-found",
  "message": "App not found",
  "parameters": {
    "app_id": "nonexistent_app"
  }
}

Additional context

Logic flow

  1. Validate app-id format
  2. Look up the App via DiscoveryManager::get_app(app_id) → 404 if not found
  3. Get app.component_id → if empty, return empty items array
  4. Look up the Component via DiscoveryManager::get_component(component_id)
  5. Get component.area → if empty, return empty items array
  6. Look up the Area via DiscoveryManager::get_area(area_id) to get its name
  7. Return the Area as an EntityReference

Existing patterns to follow

  • Follow the same handler structure as handle_app_depends_on and handle_get_hosts in discovery_handlers.cpp
  • Use the standard relationship response envelope

Route registration

Register in rest_server.cpp::setup_routes():

srv->Get((api_path("/apps") + R"(/([^/]+)/belongs-to$)"), handler);

Handler method

Add to DiscoveryHandlers:

void handle_app_belongs_to(const httplib::Request& req, httplib::Response& res);

Tests

  • Unit test: verify correct area returned for app with known component→area chain
  • Unit test: verify empty items when app has no component
  • Unit test: verify empty items when app's component has no area
  • Unit test: 404 for nonexistent app
  • Integration test: launch demo nodes, query the endpoint

Metadata

Metadata

Assignees

No one assigned

    Labels

    discoveryDiscovery endpoints or strategiesenhancementNew feature or requestgood first issueGood for newcomers

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions